[Libreoffice-commits] online.git: loolwsd/test

Henry Castro hcastro at collabora.com
Sun May 22 23:52:39 UTC 2016


 loolwsd/test/TileCacheTests.cpp |  174 ++++++++++++++++++++++++++++++++++++++++
 loolwsd/test/data/load12.ods    |binary
 loolwsd/test/helpers.hpp        |   62 ++++++++++++++
 3 files changed, 236 insertions(+)

New commits:
commit 22db071689b33310c418d5284168082b2dbf78d0
Author: Henry Castro <hcastro at collabora.com>
Date:   Sun May 22 19:53:24 2016 -0400

    loolwsd: test: add checkBlackTiles

diff --git a/loolwsd/test/TileCacheTests.cpp b/loolwsd/test/TileCacheTests.cpp
index a1a16a8..0e65a11 100644
--- a/loolwsd/test/TileCacheTests.cpp
+++ b/loolwsd/test/TileCacheTests.cpp
@@ -9,6 +9,7 @@
 
 #include "config.h"
 
+#include <png.h>
 #include <Poco/Net/WebSocket.h>
 #include <cppunit/extensions/HelperMacros.h>
 
@@ -37,6 +38,7 @@ class TileCacheTests : public CPPUNIT_NS::TestFixture
 #if ENABLE_DEBUG
     CPPUNIT_TEST(testSimultaneousTilesRenderedJustOnce);
 #endif
+    CPPUNIT_TEST(testLoad12ods);
 
     CPPUNIT_TEST_SUITE_END();
 
@@ -46,6 +48,7 @@ class TileCacheTests : public CPPUNIT_NS::TestFixture
     void testClientPartImpress();
     void testClientPartCalc();
     void testSimultaneousTilesRenderedJustOnce();
+    void testLoad12ods();
 
     void checkTiles(Poco::Net::WebSocket& socket,
                     const std::string& type);
@@ -55,6 +58,13 @@ class TileCacheTests : public CPPUNIT_NS::TestFixture
                       const int docWidth,
                       const int docHeight);
 
+    void checkBlackTiles(Poco::Net::WebSocket& socket,
+                         const int part,
+                         const int docWidth,
+                         const int docHeight);
+
+    void checkBlackTile(std::stringstream& tile);
+
     static
     std::vector<char> genRandomData(const size_t size)
     {
@@ -310,6 +320,170 @@ void TileCacheTests::testSimultaneousTilesRenderedJustOnce()
     socket2.shutdown();
 }
 
+void TileCacheTests::testLoad12ods()
+{
+    try
+    {
+        int docSheet = -1;
+        int docSheets = 0;
+        int docHeight = 0;
+        int docWidth = 0;
+
+        std::string response;
+
+         // Load a document
+        std::string documentPath, documentURL;
+        getDocumentPathAndURL("load12.ods", documentPath, documentURL);
+
+        Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
+        Poco::Net::WebSocket socket = *connectLOKit(_uri, request, _response);
+
+        sendTextFrame(socket, "load url=" + documentURL);
+        CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket));
+
+        // check document size
+        sendTextFrame(socket, "status");
+        getResponseMessage(socket, "status:", response, false);
+        CPPUNIT_ASSERT_MESSAGE("did not receive a status: message as expected", !response.empty());
+        getDocSize(response, "spreadsheet", docSheet, docSheets, docWidth, docHeight);
+
+        checkBlackTiles(socket, docSheet, docWidth, docWidth);
+    }
+    catch (const Poco::Exception& exc)
+    {
+        CPPUNIT_FAIL(exc.displayText());
+    }
+}
+
+void readTileData(png_structp png_ptr, png_bytep data, png_size_t length)
+{
+    png_voidp io_ptr = png_get_io_ptr(png_ptr);
+    CPPUNIT_ASSERT(io_ptr);
+
+    std::stringstream& streamTile = *(std::stringstream*)io_ptr;
+    streamTile.read((char*)data, length);
+}
+
+void TileCacheTests::checkBlackTile(std::stringstream& tile)
+{
+    png_uint_32 width;
+    png_uint_32 height;
+    png_uint_32 itRow;
+    png_uint_32 itCol;
+    png_uint_32 black;
+    png_uint_32 rowBytes;
+
+    png_infop ptrInfo;
+    png_infop ptrEnd;
+    png_structp ptrPNG;
+    png_byte signature[0x08];
+
+    tile.read((char *)signature, 0x08);
+    CPPUNIT_ASSERT_MESSAGE( "Tile is not recognized as a PNG", !png_sig_cmp(signature, 0x00, 0x08));
+
+    ptrPNG = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+    CPPUNIT_ASSERT_MESSAGE("png_create_read_struct failed", ptrPNG);
+
+    ptrInfo = png_create_info_struct(ptrPNG);
+    CPPUNIT_ASSERT_MESSAGE("png_create_info_struct failed", ptrInfo);
+
+    ptrEnd = png_create_info_struct(ptrPNG);
+    CPPUNIT_ASSERT_MESSAGE("png_create_info_struct failed", ptrEnd);
+
+    png_set_read_fn(ptrPNG, &tile, readTileData);
+    png_set_sig_bytes(ptrPNG, 0x08);
+
+    png_read_info(ptrPNG, ptrInfo);
+
+    width = png_get_image_width(ptrPNG, ptrInfo);
+    height = png_get_image_height(ptrPNG, ptrInfo);
+
+    png_set_interlace_handling(ptrPNG);
+    png_read_update_info(ptrPNG, ptrInfo);
+
+    rowBytes = png_get_rowbytes(ptrPNG, ptrInfo);
+    CPPUNIT_ASSERT_EQUAL(width, rowBytes / 4);
+
+    // rows
+    png_bytep rows[height];
+    for (itRow = 0; itRow < height; itRow++)
+    {
+        rows[itRow] = new png_byte[rowBytes];
+    }
+
+    png_read_image(ptrPNG, rows);
+
+    black = 0;
+    for (itRow = 0; itRow < height; itRow++)
+    {
+        itCol = 0;
+        while(itCol <= rowBytes)
+        {
+            png_byte R = rows[itRow][itCol + 0];
+            png_byte G = rows[itRow][itCol + 1];
+            png_byte B = rows[itRow][itCol + 2];
+            //png_byte A = rows[itRow][itCol + 3];
+            if (R == 0x00 && G == 0x00 && B == 0x00)
+                black++;
+
+            itCol += 4;
+        }
+    }
+
+    png_read_end(ptrPNG, ptrEnd);
+    png_destroy_read_struct(&ptrPNG, &ptrInfo, &ptrEnd);
+
+    for (itRow = 0; itRow < height; itRow++ )
+    {
+        delete rows[itRow];
+    }
+
+    CPPUNIT_ASSERT_MESSAGE("The tile is 100% black", black != height * width);
+    CPPUNIT_ASSERT_MESSAGE("The tile is 90% black", (black * 100) / (height * width) < 90);
+}
+
+void TileCacheTests::checkBlackTiles(Poco::Net::WebSocket& socket, const int part, const int docWidth, const int docHeight)
+{
+    // twips
+    const int tileSize = 3840;
+    // pixel
+    const int pixTileSize = 256;
+
+    int rows;
+    int cols;
+    int tileX;
+    int tileY;
+    int tileWidth;
+    int tileHeight;
+
+    std::string text;
+    std::vector<char> tile;
+
+    rows = docHeight / tileSize;
+    cols = docWidth / tileSize;
+
+    for (int itRow = 0; itRow < rows; ++itRow)
+    {
+        for (int itCol = 0; itCol < cols; ++itCol)
+        {
+            tileWidth = tileSize;
+            tileHeight = tileSize;
+            tileX = tileSize * itCol;
+            tileY = tileSize * itRow;
+            text = Poco::format("tile part=%d width=%d height=%d tileposx=%d tileposy=%d tilewidth=%d tileheight=%d",
+                    part, pixTileSize, pixTileSize, tileX, tileY, tileWidth, tileHeight);
+
+            sendTextFrame(socket, text);
+            tile = getTileMessage(socket, "checkBlackTiles ");
+            const std::string firstLine = LOOLProtocol::getFirstLine(tile);
+
+            std::stringstream streamTile;
+            std::copy(tile.begin() + firstLine.size() + 1, tile.end(), std::ostream_iterator<char>(streamTile));
+            checkBlackTile(streamTile);
+        }
+    }
+}
+
 void TileCacheTests::checkTiles(Poco::Net::WebSocket& socket, const std::string& docType)
 {
     const std::string current = "current=";
diff --git a/loolwsd/test/data/load12.ods b/loolwsd/test/data/load12.ods
new file mode 100644
index 0000000..36b88b7
Binary files /dev/null and b/loolwsd/test/data/load12.ods differ
diff --git a/loolwsd/test/helpers.hpp b/loolwsd/test/helpers.hpp
index 55859c6..bd26ef8 100644
--- a/loolwsd/test/helpers.hpp
+++ b/loolwsd/test/helpers.hpp
@@ -406,6 +406,68 @@ void getDocSize(const std::string& message, const std::string& type,
     CPPUNIT_ASSERT(height > 0);
 }
 
+inline
+std::vector<char> getTileMessage(Poco::Net::WebSocket& ws, const std::string& name = "")
+{
+    int flags = 0;
+    int retries = 20;
+    static const Poco::Timespan waitTime(1000000);
+    std::vector<char> response(READ_BUFFER_SIZE);
+
+    // 5 seconds timeout
+    ws.setReceiveTimeout(5000000);
+    do
+    {
+        if (ws.poll(waitTime, Poco::Net::Socket::SELECT_READ))
+        {
+            response.resize(READ_BUFFER_SIZE);
+            int bytes = ws.receiveFrame(response.data(), response.size(), flags);
+            response.resize(bytes >= 0 ? bytes : 0);
+            auto message = LOOLProtocol::getAbbreviatedMessage(response);
+            if (bytes > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
+            {
+                if (message.find("nextmessage") == 0)
+                {
+                    Poco::StringTokenizer tokens(message, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
+                    int size = 0;
+                    if (tokens.count() == 2 &&
+                        tokens[0] == "nextmessage:" && LOOLProtocol::getTokenInteger(tokens[1], "size", size) && size > 0)
+                    {
+                        std::cerr << name << " Got " << message << std::endl;
+                        response.resize(size);
+                        bytes = ws.receiveFrame(response.data(), response.size(), flags);
+                        response.resize(bytes >= 0 ? bytes : 0);
+
+                        const std::string firstLine = LOOLProtocol::getFirstLine(response);
+                        Poco::StringTokenizer tileTokens(firstLine, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
+                        CPPUNIT_ASSERT_EQUAL(std::string("tile:"), tileTokens[0]);
+                        CPPUNIT_ASSERT_EQUAL(std::string("part="), tileTokens[1].substr(0, std::string("part=").size()));
+                        CPPUNIT_ASSERT_EQUAL(std::string("width="), tileTokens[2].substr(0, std::string("width=").size()));
+                        CPPUNIT_ASSERT_EQUAL(std::string("height="), tileTokens[3].substr(0, std::string("height=").size()));
+                        CPPUNIT_ASSERT_EQUAL(std::string("tileposx="), tileTokens[4].substr(0, std::string("tileposx=").size()));
+                        CPPUNIT_ASSERT_EQUAL(std::string("tileposy="), tileTokens[5].substr(0, std::string("tileposy=").size()));
+                        CPPUNIT_ASSERT_EQUAL(std::string("tilewidth="), tileTokens[6].substr(0, std::string("tilewidth=").size()));
+                        CPPUNIT_ASSERT_EQUAL(std::string("tileheight="), tileTokens[7].substr(0, std::string("tileheight=").size()));
+                        std::cerr << name << " Got " << firstLine << std::endl;
+                        return response;
+                    }
+                }
+            }
+
+            std::cerr << name << "ignored " << message << std::endl;
+            retries = 10;
+        }
+        else
+        {
+            std::cerr << name << "Timeout\n";
+            --retries;
+        }
+    }
+    while (retries > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
+
+    return std::vector<char>();
+}
+
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */


More information about the Libreoffice-commits mailing list