/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ /* * Copyright the Collabora Online contributors. * * SPDX-License-Identifier: MPL-2.0 * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include #include #include #include #include #include #include #include #include #include /// File-Serve White-Box unit-tests. class FileServeTests : public CPPUNIT_NS::TestFixture { CPPUNIT_TEST_SUITE(FileServeTests); CPPUNIT_TEST(testUIDefaults); CPPUNIT_TEST(testCSSVars); CPPUNIT_TEST(testPreProcessedFile); CPPUNIT_TEST(testPreProcessedFileRoundtrip); CPPUNIT_TEST(testPreProcessedFileSubstitution); CPPUNIT_TEST_SUITE_END(); void testUIDefaults(); void testCSSVars(); void testPreProcessedFile(); void testPreProcessedFileRoundtrip(); void testPreProcessedFileSubstitution(); void preProcessedFileSubstitution(const std::string& testname, std::unordered_map variables); }; void FileServeTests::testUIDefaults() { constexpr auto testname = __func__; std::string uiMode; std::string uiTheme; std::string savedUIState; LOK_ASSERT_EQUAL( std::string("{\"uiMode\":\"classic\"}"), FileServerRequestHandler::uiDefaultsToJSON("UIMode=classic;huh=bleh;", uiMode, uiTheme, savedUIState)); LOK_ASSERT_EQUAL(std::string("classic"), uiMode); LOK_ASSERT_EQUAL( std::string("{\"spreadsheet\":{\"ShowSidebar\":false},\"text\":{\"ShowRuler\":true}}"), FileServerRequestHandler::uiDefaultsToJSON("TextRuler=true;SpreadsheetSidebar=false", uiMode, uiTheme, savedUIState)); LOK_ASSERT_EQUAL(std::string(""), uiMode); LOK_ASSERT_EQUAL( std::string("{\"presentation\":{\"ShowStatusbar\":false},\"spreadsheet\":{\"ShowSidebar\":" "false},\"text\":{\"ShowRuler\":true},\"uiMode\":\"notebookbar\"}"), FileServerRequestHandler::uiDefaultsToJSON( ";;UIMode=notebookbar;;PresentationStatusbar=false;;TextRuler=true;;bah=ugh;;" "SpreadsheetSidebar=false", uiMode, uiTheme, savedUIState)); LOK_ASSERT_EQUAL(std::string("{\"drawing\":{\"ShowStatusbar\":true},\"presentation\":{" "\"ShowStatusbar\":false},\"spreadsheet\":{\"ShowSidebar\":false}," "\"text\":{\"ShowRuler\":true},\"uiMode\":\"notebookbar\"}"), FileServerRequestHandler::uiDefaultsToJSON( ";;UIMode=notebookbar;;PresentationStatusbar=false;;TextRuler=true;;bah=" "ugh;;SpreadsheetSidebar=false;;DrawingStatusbar=true", uiMode, uiTheme, savedUIState)); LOK_ASSERT_EQUAL(std::string("notebookbar"), uiMode); } void FileServeTests::testCSSVars() { constexpr auto testname = __func__; LOK_ASSERT_EQUAL( std::string(""), FileServerRequestHandler::cssVarsToStyle( "--co-somestyle-text=#123456;--co-somestyle-size=15px;")); LOK_ASSERT_EQUAL( std::string(""), FileServerRequestHandler::cssVarsToStyle( ";;--co-somestyle-text=#123456;;--co-somestyle-size=15px;;;")); LOK_ASSERT_EQUAL( std::string(""), FileServerRequestHandler::cssVarsToStyle( "--co-somestyle-text=#123456;;--co-somestyle-size=15px;--co-sometext#324;;")); LOK_ASSERT_EQUAL(std::string(""), FileServerRequestHandler::cssVarsToStyle( "--co-somestyle-text=#123456;;--some-val=3453--some-other-val=4536;;")); } /// Tests file pre-processing through PreProcessedFile class. void FileServeTests::testPreProcessedFile() { constexpr auto testname = __func__; { const std::string data = "Data %VAR% Data"; const PreProcessedFile ppf("filename", data); LOK_ASSERT_EQUAL(ppf.filename(), std::string("filename")); LOK_ASSERT_EQUAL(ppf.size(), data.size()); LOK_ASSERT_EQUAL(3UL, ppf._segments.size()); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Data, ppf._segments[0].first); LOK_ASSERT_EQUAL(std::string("Data "), ppf._segments[0].second); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Variable, ppf._segments[1].first); LOK_ASSERT_EQUAL(std::string("VAR"), ppf._segments[1].second); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Data, ppf._segments[2].first); LOK_ASSERT_EQUAL(std::string(" Data"), ppf._segments[2].second); } { const std::string data = "Data %VAR%"; const PreProcessedFile ppf("filename", data); LOK_ASSERT_EQUAL(ppf.filename(), std::string("filename")); LOK_ASSERT_EQUAL(ppf.size(), data.size()); LOK_ASSERT_EQUAL(2UL, ppf._segments.size()); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Data, ppf._segments[0].first); LOK_ASSERT_EQUAL(std::string("Data "), ppf._segments[0].second); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Variable, ppf._segments[1].first); LOK_ASSERT_EQUAL(std::string("VAR"), ppf._segments[1].second); } { const std::string data = "%VAR% Data"; const PreProcessedFile ppf("filename", data); LOK_ASSERT_EQUAL(ppf.filename(), std::string("filename")); LOK_ASSERT_EQUAL(ppf.size(), data.size()); LOK_ASSERT_EQUAL(2UL, ppf._segments.size()); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Variable, ppf._segments[0].first); LOK_ASSERT_EQUAL(std::string("VAR"), ppf._segments[0].second); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Data, ppf._segments[1].first); LOK_ASSERT_EQUAL(std::string(" Data"), ppf._segments[1].second); } { const std::string data = "%VAR%"; const PreProcessedFile ppf("filename", data); LOK_ASSERT_EQUAL(ppf.filename(), std::string("filename")); LOK_ASSERT_EQUAL(ppf.size(), data.size()); LOK_ASSERT_EQUAL(1UL, ppf._segments.size()); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Variable, ppf._segments[0].first); LOK_ASSERT_EQUAL(std::string("VAR"), ppf._segments[0].second); } { const std::string data = "%VAR%Data1 %VAR% Data%VAR%"; const PreProcessedFile ppf("filename", data); LOK_ASSERT_EQUAL(ppf.filename(), std::string("filename")); LOK_ASSERT_EQUAL(ppf.size(), data.size()); LOK_ASSERT_EQUAL(5UL, ppf._segments.size()); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Variable, ppf._segments[0].first); LOK_ASSERT_EQUAL(std::string("VAR"), ppf._segments[0].second); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Data, ppf._segments[1].first); LOK_ASSERT_EQUAL(std::string("Data1 "), ppf._segments[1].second); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Variable, ppf._segments[2].first); LOK_ASSERT_EQUAL(std::string("VAR"), ppf._segments[2].second); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Data, ppf._segments[3].first); LOK_ASSERT_EQUAL(std::string(" Data"), ppf._segments[3].second); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Variable, ppf._segments[4].first); LOK_ASSERT_EQUAL(std::string("VAR"), ppf._segments[4].second); } { const std::string data = "Data %VAR Data"; const PreProcessedFile ppf("filename", data); LOK_ASSERT_EQUAL(ppf.filename(), std::string("filename")); LOK_ASSERT_EQUAL(ppf.size(), data.size()); LOK_ASSERT_EQUAL(1UL, ppf._segments.size()); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Data, ppf._segments[0].first); LOK_ASSERT_EQUAL(std::string("Data %VAR Data"), ppf._segments[0].second); } { const std::string data = "Data 5% Data 7%"; const PreProcessedFile ppf("filename", data); LOK_ASSERT_EQUAL(ppf.filename(), std::string("filename")); LOK_ASSERT_EQUAL(ppf.size(), data.size()); LOK_ASSERT_EQUAL(1UL, ppf._segments.size()); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Data, ppf._segments[0].first); LOK_ASSERT_EQUAL(std::string("Data 5% Data 7%"), ppf._segments[0].second); } { const std::string data = "Data Data"; const PreProcessedFile ppf("filename", data); LOK_ASSERT_EQUAL(ppf.filename(), std::string("filename")); LOK_ASSERT_EQUAL(ppf.size(), data.size()); LOK_ASSERT_EQUAL(3UL, ppf._segments.size()); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Data, ppf._segments[0].first); LOK_ASSERT_EQUAL(std::string("Data "), ppf._segments[0].second); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::CommentedVariable, ppf._segments[1].first); LOK_ASSERT_EQUAL(std::string("VAR"), ppf._segments[1].second); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Data, ppf._segments[2].first); LOK_ASSERT_EQUAL(std::string(" Data"), ppf._segments[2].second); } { const std::string data = "Data "; const PreProcessedFile ppf("filename", data); LOK_ASSERT_EQUAL(ppf.filename(), std::string("filename")); LOK_ASSERT_EQUAL(ppf.size(), data.size()); LOK_ASSERT_EQUAL(2UL, ppf._segments.size()); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Data, ppf._segments[0].first); LOK_ASSERT_EQUAL(std::string("Data "), ppf._segments[0].second); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::CommentedVariable, ppf._segments[1].first); LOK_ASSERT_EQUAL(std::string("VAR"), ppf._segments[1].second); } { const std::string data = " Data"; const PreProcessedFile ppf("filename", data); LOK_ASSERT_EQUAL(ppf.filename(), std::string("filename")); LOK_ASSERT_EQUAL(ppf.size(), data.size()); LOK_ASSERT_EQUAL(2UL, ppf._segments.size()); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::CommentedVariable, ppf._segments[0].first); LOK_ASSERT_EQUAL(std::string("VAR"), ppf._segments[0].second); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Data, ppf._segments[1].first); LOK_ASSERT_EQUAL(std::string(" Data"), ppf._segments[1].second); } { const std::string data = ""; const PreProcessedFile ppf("filename", data); LOK_ASSERT_EQUAL(ppf.filename(), std::string("filename")); LOK_ASSERT_EQUAL(ppf.size(), data.size()); LOK_ASSERT_EQUAL(1UL, ppf._segments.size()); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::CommentedVariable, ppf._segments[0].first); LOK_ASSERT_EQUAL(std::string("VAR"), ppf._segments[0].second); } { const std::string data = "Data1 Data2"; const PreProcessedFile ppf("filename", data); LOK_ASSERT_EQUAL(ppf.filename(), std::string("filename")); LOK_ASSERT_EQUAL(ppf.size(), data.size()); LOK_ASSERT_EQUAL(5UL, ppf._segments.size()); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::CommentedVariable, ppf._segments[0].first); LOK_ASSERT_EQUAL(std::string("VAR"), ppf._segments[0].second); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Data, ppf._segments[1].first); LOK_ASSERT_EQUAL(std::string("Data1 "), ppf._segments[1].second); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::CommentedVariable, ppf._segments[2].first); LOK_ASSERT_EQUAL(std::string("VAR"), ppf._segments[2].second); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Data, ppf._segments[3].first); LOK_ASSERT_EQUAL(std::string(" Data2"), ppf._segments[3].second); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::CommentedVariable, ppf._segments[4].first); LOK_ASSERT_EQUAL(std::string("VAR"), ppf._segments[4].second); } { const std::string data = " Data4"; const PreProcessedFile ppf("filename", data); LOK_ASSERT_EQUAL(ppf.filename(), std::string("filename")); LOK_ASSERT_EQUAL(ppf.size(), data.size()); LOK_ASSERT_EQUAL(4UL, ppf._segments.size()); LOK_ASSERT_EQUAL(PreProcessedFile::SegmentType::Data, ppf._segments[0].first); LOK_ASSERT_EQUAL(std::string("Data1 Data2 Online Editor