loolwsd/LOOLSession.cpp | 92 +++++++++++++--------------------- loolwsd/LOOLSession.hpp | 3 + loolwsd/test/data/hide-whitespace.odt |binary loolwsd/test/httpwstest.cpp | 78 ++++++++++++++++++++++++---- 4 files changed, 105 insertions(+), 68 deletions(-)
New commits: commit 0723a96df74f9d6d6d55aa7315769d01be98fecd Author: Miklos Vajna <vmik...@collabora.co.uk> Date: Thu Nov 19 11:11:47 2015 +0100 HTTPWSTest: share socket init code between tests diff --git a/loolwsd/test/httpwstest.cpp b/loolwsd/test/httpwstest.cpp index 35a4870..c967fff 100644 --- a/loolwsd/test/httpwstest.cpp +++ b/loolwsd/test/httpwstest.cpp @@ -21,6 +21,12 @@ /// Tests the HTTP WebSocket API of loolwsd. The server has to be started manually before running this test. class HTTPWSTest : public CPPUNIT_NS::TestFixture { + Poco::URI _uri; + Poco::Net::HTTPClientSession _session; + Poco::Net::HTTPRequest _request; + Poco::Net::HTTPResponse _response; + Poco::Net::WebSocket _socket; + CPPUNIT_TEST_SUITE(HTTPWSTest); CPPUNIT_TEST(testPaste); CPPUNIT_TEST(testRenderingOptions); @@ -30,35 +36,38 @@ class HTTPWSTest : public CPPUNIT_NS::TestFixture void testRenderingOptions(); void sendTextFrame(Poco::Net::WebSocket& socket, const std::string& string); +public: + HTTPWSTest() + : _uri("http://127.0.0.1:" + std::to_string(LOOLWSD::DEFAULT_CLIENT_PORT_NUMBER)), + _session(_uri.getHost(), _uri.getPort()), + _request(Poco::Net::HTTPRequest::HTTP_POST, "/ws"), + _socket(_session, _request, _response) + { + } }; void HTTPWSTest::testPaste() { // Load a document and make it empty. - Poco::URI uri("http://127.0.0.1:" + std::to_string(LOOLWSD::DEFAULT_CLIENT_PORT_NUMBER)); - Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort()); - Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/ws"); - Poco::Net::HTTPResponse response; - Poco::Net::WebSocket socket(session, request, response); std::string documentPath = TDOC "/hello.odt"; std::string documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString(); - sendTextFrame(socket, "load url=" + documentURL); - sendTextFrame(socket, "uno .uno:SelectAll"); - sendTextFrame(socket, "uno .uno:Delete"); + sendTextFrame(_socket, "load url=" + documentURL); + sendTextFrame(_socket, "uno .uno:SelectAll"); + sendTextFrame(_socket, "uno .uno:Delete"); // Paste some text into it. - sendTextFrame(socket, "paste mimetype=text/plain;charset=utf-8 data=aaa bbb ccc"); + sendTextFrame(_socket, "paste mimetype=text/plain;charset=utf-8 data=aaa bbb ccc"); // Check if the document contains the pasted text. - sendTextFrame(socket, "uno .uno:SelectAll"); - sendTextFrame(socket, "gettextselection mimetype=text/plain;charset=utf-8"); + sendTextFrame(_socket, "uno .uno:SelectAll"); + sendTextFrame(_socket, "gettextselection mimetype=text/plain;charset=utf-8"); std::string selection; int flags; int n; do { char buffer[100000]; - n = socket.receiveFrame(buffer, sizeof(buffer), flags); + n = _socket.receiveFrame(buffer, sizeof(buffer), flags); if (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE) { std::string line = LOOLProtocol::getFirstLine(buffer, n); @@ -71,23 +80,18 @@ void HTTPWSTest::testPaste() } } while (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE); - socket.shutdown(); + _socket.shutdown(); CPPUNIT_ASSERT_EQUAL(std::string("aaa bbb ccc"), selection); } void HTTPWSTest::testRenderingOptions() { // Load a document and get its size. - Poco::URI uri("http://127.0.0.1:" + std::to_string(LOOLWSD::DEFAULT_CLIENT_PORT_NUMBER)); - Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort()); - Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/ws"); - Poco::Net::HTTPResponse response; - Poco::Net::WebSocket socket(session, request, response); std::string documentPath = TDOC "/hide-whitespace.odt"; std::string documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString(); std::string options = "{\"rendering\":{\".uno:HideWhitespace\":{\"type\":\"boolean\",\"value\":\"true\"}}}"; - sendTextFrame(socket, "load url=" + documentURL + " options=" + options); - sendTextFrame(socket, "status"); + sendTextFrame(_socket, "load url=" + documentURL + " options=" + options); + sendTextFrame(_socket, "status"); std::string status; int flags; @@ -95,7 +99,7 @@ void HTTPWSTest::testRenderingOptions() do { char buffer[100000]; - n = socket.receiveFrame(buffer, sizeof(buffer), flags); + n = _socket.receiveFrame(buffer, sizeof(buffer), flags); if (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE) { std::string line = LOOLProtocol::getFirstLine(buffer, n); @@ -108,7 +112,7 @@ void HTTPWSTest::testRenderingOptions() } } while (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE); - socket.shutdown(); + _socket.shutdown(); // Expected format is something like 'type=text parts=2 current=0 width=12808 height=1142'. Poco::StringTokenizer tokens(status, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM); CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(5), tokens.count()); commit 7be61ec464ad16d22baf9bbeb5a4aa66a07e6fe8 Author: Miklos Vajna <vmik...@collabora.co.uk> Date: Thu Nov 19 11:01:34 2015 +0100 HTTPWSTest: add document rendering options testcase Fails without commit 57199a36cdc2ad802f8d7e944e6fc4ae830fab26 (loolwsd: route rendering options to initializeForRendering(), 2015-11-19). diff --git a/loolwsd/test/data/hide-whitespace.odt b/loolwsd/test/data/hide-whitespace.odt new file mode 100644 index 0000000..7fbd17b Binary files /dev/null and b/loolwsd/test/data/hide-whitespace.odt differ diff --git a/loolwsd/test/httpwstest.cpp b/loolwsd/test/httpwstest.cpp index 2cb08fd..35a4870 100644 --- a/loolwsd/test/httpwstest.cpp +++ b/loolwsd/test/httpwstest.cpp @@ -11,6 +11,7 @@ #include <Poco/Net/HTTPRequest.h> #include <Poco/Net/HTTPResponse.h> #include <Poco/Net/WebSocket.h> +#include <Poco/StringTokenizer.h> #include <Poco/URI.h> #include <cppunit/extensions/HelperMacros.h> @@ -22,9 +23,11 @@ class HTTPWSTest : public CPPUNIT_NS::TestFixture { CPPUNIT_TEST_SUITE(HTTPWSTest); CPPUNIT_TEST(testPaste); + CPPUNIT_TEST(testRenderingOptions); CPPUNIT_TEST_SUITE_END(); void testPaste(); + void testRenderingOptions(); void sendTextFrame(Poco::Net::WebSocket& socket, const std::string& string); }; @@ -72,6 +75,51 @@ void HTTPWSTest::testPaste() CPPUNIT_ASSERT_EQUAL(std::string("aaa bbb ccc"), selection); } +void HTTPWSTest::testRenderingOptions() +{ + // Load a document and get its size. + Poco::URI uri("http://127.0.0.1:" + std::to_string(LOOLWSD::DEFAULT_CLIENT_PORT_NUMBER)); + Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort()); + Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/ws"); + Poco::Net::HTTPResponse response; + Poco::Net::WebSocket socket(session, request, response); + std::string documentPath = TDOC "/hide-whitespace.odt"; + std::string documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString(); + std::string options = "{\"rendering\":{\".uno:HideWhitespace\":{\"type\":\"boolean\",\"value\":\"true\"}}}"; + sendTextFrame(socket, "load url=" + documentURL + " options=" + options); + sendTextFrame(socket, "status"); + + std::string status; + int flags; + int n; + do + { + char buffer[100000]; + n = socket.receiveFrame(buffer, sizeof(buffer), flags); + if (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE) + { + std::string line = LOOLProtocol::getFirstLine(buffer, n); + std::string prefix = "status: "; + if (line.find(prefix) == 0) + { + status = line.substr(prefix.length()); + break; + } + } + } + while (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE); + socket.shutdown(); + // Expected format is something like 'type=text parts=2 current=0 width=12808 height=1142'. + Poco::StringTokenizer tokens(status, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(5), tokens.count()); + std::string token = tokens[4]; + std::string prefix = "height="; + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), token.find(prefix)); + int height = std::stoi(token.substr(prefix.size())); + // HideWhitespace was ignored, this was 32532. + CPPUNIT_ASSERT(height < 10000); +} + void HTTPWSTest::sendTextFrame(Poco::Net::WebSocket& socket, const std::string& string) { socket.sendFrame(string.data(), string.size()); commit b066c91c8f8542b9348911cb714c6a82c081eefc Author: Miklos Vajna <vmik...@collabora.co.uk> Date: Thu Nov 19 10:30:00 2015 +0100 loolwsd: factor out LOOLSession::parseDocOptions() from *ProcessSession diff --git a/loolwsd/LOOLSession.cpp b/loolwsd/LOOLSession.cpp index efe4cf4..ff0b316 100644 --- a/loolwsd/LOOLSession.cpp +++ b/loolwsd/LOOLSession.cpp @@ -132,6 +132,40 @@ void LOOLSession::sendBinaryFrame(const char *buffer, int length) _ws->sendFrame(buffer, length, WebSocket::FRAME_BINARY); } +void LOOLSession::parseDocOptions(const StringTokenizer& tokens, int& part, std::string& timestamp) +{ + // First token is the "load" command itself. + size_t offset = 1; + if (tokens.count() > 2 && tokens[1].find("part=") == 0) + { + getTokenInteger(tokens[1], "part", part); + ++offset; + } + + for (size_t i = offset; i < tokens.count(); ++i) + { + if (tokens[i].find("url=") == 0) + { + _docURL = tokens[i].substr(strlen("url=")); + ++offset; + } + else if (tokens[i].find("timestamp=") == 0) + { + timestamp = tokens[i].substr(strlen("timestamp=")); + ++offset; + } + } + + if (tokens.count() > offset) + { + if (getTokenString(tokens[offset], "options", _docOptions)) + { + if (tokens.count() > offset + 1) + _docOptions += Poco::cat(std::string(" "), tokens.begin() + offset + 1, tokens.end()); + } + } +} + std::map<Process::PID, UInt64> MasterProcessSession::_childProcesses; std::set<std::shared_ptr<MasterProcessSession>> MasterProcessSession::_availableChildSessions; @@ -433,37 +467,8 @@ bool MasterProcessSession::loadDocument(const char* /*buffer*/, int /*length*/, return false; } - // First token is the "load" command itself. - size_t offset = 1; - if (tokens.count() > 2 && tokens[1].find("part=") == 0) - { - getTokenInteger(tokens[1], "part", _loadPart); - ++offset; - } - std::string timestamp; - for (size_t i = offset; i < tokens.count(); ++i) - { - if (tokens[i].find("url=") == 0) - { - _docURL = tokens[i].substr(strlen("url=")); - ++offset; - } - else if (tokens[i].find("timestamp=") == 0) - { - timestamp = tokens[i].substr(strlen("timestamp=")); - ++offset; - } - } - - if (tokens.count() > offset) - { - if (getTokenString(tokens[offset], "options", _docOptions)) - { - if (tokens.count() > offset + 1) - _docOptions += Poco::cat(std::string(" "), tokens.begin() + offset + 1, tokens.end()); - } - } + parseDocOptions(tokens, _loadPart, timestamp); try { @@ -967,31 +972,8 @@ bool ChildProcessSession::loadDocument(const char *buffer, int length, StringTok return false; } - // First token is the "load" command itself. - size_t offset = 1; - if (tokens.count() > 2 && tokens[1].find("part=") == 0) - { - getTokenInteger(tokens[1], "part", part); - ++offset; - } - - for (size_t i = offset; i < tokens.count(); ++i) - { - if (tokens[i].find("url=") == 0) - { - _docURL = tokens[i].substr(strlen("url=")); - ++offset; - } - } - - if (tokens.count() > offset) - { - if (getTokenString(tokens[offset], "options", _docOptions)) - { - if (tokens.count() > offset + 1) - _docOptions += Poco::cat(std::string(" "), tokens.begin() + offset + 1, tokens.end()); - } - } + std::string timestamp; + parseDocOptions(tokens, part, timestamp); URI aUri; try diff --git a/loolwsd/LOOLSession.hpp b/loolwsd/LOOLSession.hpp index 4dbb1cf..b807cab 100644 --- a/loolwsd/LOOLSession.hpp +++ b/loolwsd/LOOLSession.hpp @@ -64,6 +64,9 @@ protected: void sendBinaryFrame(const char *buffer, int length); + /// Parses the options of the "load" command, shared between MasterProcessSession::loadDocument() and ChildProcessSession::loadDocument(). + void parseDocOptions(const Poco::StringTokenizer& tokens, int& part, std::string& timestamp); + virtual bool loadDocument(const char *buffer, int length, Poco::StringTokenizer& tokens) = 0; virtual void sendTile(const char *buffer, int length, Poco::StringTokenizer& tokens) = 0; _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/libreoffice-commits