Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(TEST_LIBRARIES
if(NOT TIDE_ENABLE_REST_INTERFACE)
set(EXCLUDE_FROM_TESTS
core/JsonOptionsTests.cpp
core/LoggingUtilityTest.cpp
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alphabetical order, move up

core/RESTCommandTests.cpp
)
endif()
Expand Down
214 changes: 214 additions & 0 deletions tests/cpp/core/LoggingUtilityTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/*********************************************************************/
/* Copyright (c) 2016, EPFL/Blue Brain Project */
/* Pawel Podhajski <pawel.podhajski@epfl.ch> */
/* All rights reserved. */
/* */
/* Redistribution and use in source and binary forms, with or */
/* without modification, are permitted provided that the following */
/* conditions are met: */
/* */
/* 1. Redistributions of source code must retain the above */
/* copyright notice, this list of conditions and the following */
/* disclaimer. */
/* */
/* 2. Redistributions in binary form must reproduce the above */
/* copyright notice, this list of conditions and the following */
/* disclaimer in the documentation and/or other materials */
/* provided with the distribution. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
/* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
/* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
/* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
/* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
/* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
/* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
/* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
/* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
/* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
/* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
/* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
/* POSSIBILITY OF SUCH DAMAGE. */
/* */
/* The views and conclusions contained in the software and */
/* documentation are those of the authors and should not be */
/* interpreted as representing official policies, either expressed */
/* or implied, of Ecole polytechnique federale de Lausanne. */
/*********************************************************************/

#define BOOST_TEST_MODULE LoggingUtilityTest

#include <boost/make_shared.hpp>
#include <boost/regex.hpp>
#include <boost/test/unit_test.hpp>
namespace ut = boost::unit_test;

#include "DisplayGroup.h"
#include "DummyContent.h"
#include "LoggingUtility.h"
#include "rest/RestLogger.h"

#include <QObject>

namespace
{
const QSize wallSize( 1000, 1000 );
const std::string regexJson{
R"(\{
"event": \{
"count": 2,
"last_event": "contentWindowAdded",
"last_event_date": "\d{4}-\d{2}-\d{2}\u\d{2}:\d{2}:\d{2}.\d{6}"
\},
"window": \{
"accumulated_count": 2,
"count": 2,
"date_set": "\d{4}-\d{2}-\d{2}\u\d{2}:\d{2}:\d{2}.\d{6}"
\}
\}
)"
};
const std::string defaultJson{
R"({
"event": {
"count": 0,
"last_event": "",
"last_event_date": ""
},
"window": {
"accumulated_count": 0,
"count": 0,
"date_set": ""
}
}
)"
};
}

BOOST_AUTO_TEST_CASE( testAccumulatedWindowCount )
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The json uses "total count" instead of "accumulated count", can we choose one or the other?

{
ContentPtr content( new DummyContent );
DisplayGroupPtr displayGroup( new DisplayGroup( wallSize ));

ContentWindowPtr window1 = boost::make_shared<ContentWindow>( content );
ContentWindowPtr window2 = boost::make_shared<ContentWindow>( content );
std::unique_ptr<LoggingUtility> logger = make_unique<LoggingUtility>();

QObject::connect( displayGroup.get(), &DisplayGroup::contentWindowAdded,
logger.get(), &LoggingUtility::contentWindowAdded );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good, also do BOOST_CHECK( logger.get()->getAccumulatedWindowCount( ) == 0 ); before adding a window

BOOST_CHECK( logger.get()->getAccumulatedWindowCount() == 0);
displayGroup->addContentWindow( window1 );
BOOST_CHECK( logger.get()->getAccumulatedWindowCount() == 1);

displayGroup->addContentWindow( window2 );
BOOST_CHECK( logger.get()->getAccumulatedWindowCount() == 2);

}

BOOST_AUTO_TEST_CASE( testWindowCount )
{
ContentPtr content( new DummyContent );
DisplayGroupPtr displayGroup( new DisplayGroup( wallSize ));

ContentWindowPtr window1 = boost::make_shared<ContentWindow>( content );
std::unique_ptr<LoggingUtility> logger = make_unique<LoggingUtility>();

QObject::connect( displayGroup.get(), &DisplayGroup::contentWindowAdded,
logger.get(), &LoggingUtility::contentWindowAdded );
QObject::connect( displayGroup.get(), &DisplayGroup::contentWindowRemoved,
logger.get(), &LoggingUtility::contentWindowRemoved );
BOOST_CHECK( logger.get()->getWindowCount() == 0);
displayGroup->addContentWindow( window1 );
BOOST_CHECK( logger.get()->getWindowCount() == 1);
displayGroup->removeContentWindow( window1 );
BOOST_CHECK( logger.get()->getWindowCount() == 0);
}

BOOST_AUTO_TEST_CASE( testWindowCountDecrement )
{
ContentPtr content( new DummyContent );
DisplayGroupPtr displayGroup( new DisplayGroup( wallSize ));

ContentWindowPtr window1 = boost::make_shared<ContentWindow>( content );
std::unique_ptr<LoggingUtility> logger = make_unique<LoggingUtility>();

BOOST_CHECK( logger.get()->getWindowCount() == 0);
displayGroup->addContentWindow( window1 );
QObject::connect( displayGroup.get(), &DisplayGroup::contentWindowRemoved,
logger.get(), &LoggingUtility::contentWindowRemoved );
displayGroup->removeContentWindow( window1 );
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not correctly test that the counter cannot be negative, because removing the same window a second time does not emit a windowRemoved signal. Do a separate boost test case where you add a window before connecting the windowAdded signal, then remove the window.

BOOST_CHECK( logger.get()->getWindowCount() == 0);
}

BOOST_AUTO_TEST_CASE( testInteractionCounter )
{
ContentPtr content( new DummyContent );
DisplayGroupPtr displayGroup( new DisplayGroup( wallSize ));

ContentWindowPtr window1 = boost::make_shared<ContentWindow>( content );
ContentWindowPtr window2 = boost::make_shared<ContentWindow>( content );
std::unique_ptr<LoggingUtility> logger = make_unique<LoggingUtility>();

BOOST_CHECK( logger.get()->getInteractionCount() == 0);

QObject::connect( displayGroup.get(), &DisplayGroup::contentWindowAdded,
logger.get(), &LoggingUtility::contentWindowAdded );

displayGroup->addContentWindow( window1 );
displayGroup->focus( window1->getID());
BOOST_CHECK( logger.get()->getInteractionCount() == 2);

}

BOOST_AUTO_TEST_CASE( testLastInteraction )
{
ContentPtr content( new DummyContent );
DisplayGroupPtr displayGroup( new DisplayGroup( wallSize ));

ContentWindowPtr window1 = boost::make_shared<ContentWindow>( content );
std::unique_ptr<LoggingUtility> logger = make_unique<LoggingUtility>();
BOOST_CHECK( logger.get()->getLastInteraction() == "");

QObject::connect( displayGroup.get(), &DisplayGroup::contentWindowAdded,
logger.get(), &LoggingUtility::contentWindowAdded );

displayGroup->addContentWindow( window1 );
displayGroup->focus( window1->getID());
BOOST_CHECK( logger.get()->getLastInteraction() == "mode changed");

}

BOOST_AUTO_TEST_CASE( testJsonOutput )
{
ContentPtr content( new DummyContent );
DisplayGroupPtr displayGroup( new DisplayGroup( wallSize ));

ContentWindowPtr window1 = boost::make_shared<ContentWindow>( content );
ContentWindowPtr window2 = boost::make_shared<ContentWindow>( content );
std::unique_ptr<LoggingUtility> logger = make_unique<LoggingUtility>();

QObject::connect( displayGroup.get(), &DisplayGroup::contentWindowAdded,
logger.get(), &LoggingUtility::contentWindowAdded );

std::unique_ptr<RestLogger> logContent;
logContent.reset( new RestLogger( *(logger.get()) ));
BOOST_CHECK( logContent->toJSON() == defaultJson );

displayGroup->addContentWindow( window1 );
displayGroup->addContentWindow( window2 );

std::string logText = logContent->toJSON();

boost::regex ip_regex(regexJson);
boost::sregex_iterator it(logText.begin(), logText.end(), ip_regex);
boost::sregex_iterator end;

std::string outputJson;
for (; it != end; ++it)
outputJson.append( it->str());

BOOST_CHECK( logContent->toJSON() == outputJson );

}
2 changes: 2 additions & 0 deletions tide/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ list(APPEND TIDECORE_PUBLIC_HEADERS
LayoutEngine.h
LodTools.h
log.h
LoggingUtility.h
Markers.h
MasterFromWallChannel.h
MasterToWallChannel.h
Expand Down Expand Up @@ -116,6 +117,7 @@ list(APPEND TIDECORE_SOURCES
LayoutEngine.cpp
LodTools.cpp
log.cpp
LoggingUtility.cpp
Markers.cpp
MasterFromWallChannel.cpp
MasterToWallChannel.cpp
Expand Down
113 changes: 113 additions & 0 deletions tide/core/LoggingUtility.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*********************************************************************/
/* Copyright (c) 2016, EPFL/Blue Brain Project */
/* Pawel Podhajski <pawel.podhajski@epfl.ch> */
/* All rights reserved. */
/* */
/* Redistribution and use in source and binary forms, with or */
/* without modification, are permitted provided that the following */
/* conditions are met: */
/* */
/* 1. Redistributions of source code must retain the above */
/* copyright notice, this list of conditions and the following */
/* disclaimer. */
/* */
/* 2. Redistributions in binary form must reproduce the above */
/* copyright notice, this list of conditions and the following */
/* disclaimer in the documentation and/or other materials */
/* provided with the distribution. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
/* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
/* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
/* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
/* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
/* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
/* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
/* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
/* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
/* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
/* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
/* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
/* POSSIBILITY OF SUCH DAMAGE. */
/* */
/* The views and conclusions contained in the software and */
/* documentation are those of the authors and should not be */
/* interpreted as representing official policies, either expressed */
/* or implied, of Ecole polytechnique federale de Lausanne. */
/*********************************************************************/

#include "LoggingUtility.h"
#include "ContentWindow.h"

#include <boost/date_time/posix_time/posix_time.hpp>

size_t LoggingUtility::getAccumulatedWindowCount() const
{
return _windowCounterTotal;
};

const QString& LoggingUtility::getCounterModificationTime() const
{
return _counterModificationTime;
};

int LoggingUtility::getInteractionCount() const
{
return _interactionCounter;
};

const QString& LoggingUtility::getLastInteraction() const
{
return _lastInteraction;
};

const QString& LoggingUtility::getLastInteractionTime() const
{
return _lastInteractionTime;
};

size_t LoggingUtility::getWindowCount() const
{
return _windowCounter;
};

void LoggingUtility::contentWindowAdded( ContentWindowPtr contentWindow )
{
connect( contentWindow.get(), &ContentWindow::stateChanged,
[this]() { _log( "state changed" ); });
connect( contentWindow.get(), &ContentWindow::modeChanged,
[this]() { _log( "mode changed" ); });

++_windowCounter;
++_windowCounterTotal;
_counterModificationTime=_getTimeStamp();
_log(__func__);
};

void LoggingUtility::contentWindowMovedToFront()
{
_log(__func__);
};

void LoggingUtility::contentWindowRemoved()
{
if (_windowCounter > 0)
--_windowCounter;
_counterModificationTime = _getTimeStamp();
_log( __func__ );
};

QString LoggingUtility::_getTimeStamp() const
{
using namespace boost::posix_time;
ptime t = microsec_clock::local_time();
return QString::fromStdString(to_iso_extended_string(t));
};

void LoggingUtility::_log( const QString& s )
{
_lastInteraction = s;
_lastInteractionTime = _getTimeStamp();
++_interactionCounter;
};
Loading