Skip to content

Commit

Permalink
[CImgPlugin] Fix the failing test.
Browse files Browse the repository at this point in the history
It took me a very long time to locate the problem. It was in fact
very simple. ImageCImg/ImagePNG where in fact not inheriting from Sofa_test
and manually clearing the MessageDispatcher's list preventing the
MainGTestMessageHandler to be active.
  • Loading branch information
damienmarchal committed Apr 24, 2017
1 parent fd7a3aa commit e65ef09
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 46 deletions.
14 changes: 5 additions & 9 deletions SofaKernel/framework/framework_test/helper/io/ImagePNG_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,11 @@
#include <sofa/helper/system/FileRepository.h>
#include <cstring>

#include <gtest/gtest.h>

#include <SofaTest/TestMessageHandler.h>
using sofa::helper::logging::MessageDispatcher ;
using sofa::helper::logging::Message ;

#include <SofaTest/Sofa_test.h>

namespace sofa {


class ImagePNG_test : public ::testing::Test
class ImagePNG_test : public Sofa_test<>
{
protected:

Expand Down Expand Up @@ -76,7 +70,6 @@ class ImagePNG_test : public ::testing::Test
EXPECT_TRUE(0 == std::memcmp(data, testdata, width*height*bpp));
}
};

};

TEST_F(ImagePNG_test, ImagePNG_NoFile)
Expand All @@ -90,6 +83,9 @@ TEST_F(ImagePNG_test, ImagePNG_NoFile)

TEST_F(ImagePNG_test, ImagePNG_NoImg)
{
/// This generate a test failure if no error message is generated.
EXPECT_MSG_EMIT(Error) ;

sofa::helper::io::ImagePNG imgNoImage;
EXPECT_FALSE(imgNoImage.load("image/imagetest_noimage.png"));
}
Expand Down
22 changes: 15 additions & 7 deletions SofaKernel/framework/sofa/helper/logging/MessageDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,43 +109,51 @@ class MessageDispatcherImpl

void process(sofa::helper::logging::Message& m)
{
for( size_t i=0 ; i<m_messageHandlers.size() ; i++ )
for( size_t i=0 ; i<m_messageHandlers.size() ; i++ ){
m_messageHandlers[i]->process(m) ;
}
}
};


MessageDispatcherImpl s_messagedispatcher ;
MessageDispatcherImpl* s_messagedispatcher = nullptr ;

MessageDispatcherImpl* getMainInstance(){
if(s_messagedispatcher==nullptr){
s_messagedispatcher = new MessageDispatcherImpl();
}
return s_messagedispatcher;
}

std::vector<MessageHandler*>& MessageDispatcher::getHandlers()
{
//lock_guard<mutex> guard(getMutex()) ;

return s_messagedispatcher.getHandlers();
return getMainInstance()->getHandlers();
}

int MessageDispatcher::addHandler(MessageHandler* o){
//lock_guard<mutex> guard(getMutex()) ;

return s_messagedispatcher.addHandler(o);
return getMainInstance()->addHandler(o);
}

int MessageDispatcher::rmHandler(MessageHandler* o){
//lock_guard<mutex> guard(getMutex()) ;

return s_messagedispatcher.rmHandler(o);
return getMainInstance()->rmHandler(o);
}

void MessageDispatcher::clearHandlers(){
//lock_guard<mutex> guard(getMutex()) ;

s_messagedispatcher.clearHandlers();
getMainInstance()->clearHandlers();
}

void MessageDispatcher::process(sofa::helper::logging::Message& m){
//lock_guard<mutex> guard(getMutex()) ;

s_messagedispatcher.process(m);
getMainInstance()->process(m);
}


Expand Down
35 changes: 15 additions & 20 deletions applications/plugins/CImgPlugin/CImgPlugin_test/ImageCImg_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,18 @@
#include <numeric>
#include <gtest/gtest.h>

#include <SofaTest/TestMessageHandler.h>
using sofa::helper::logging::MessageDispatcher ;
using sofa::helper::logging::ExpectMessage ;
using sofa::helper::logging::Message ;

#include <sofa/helper/logging/LoggingMessageHandler.h>
using sofa::helper::logging::MainLoggingMessageHandler ;

#include <sofa/helper/logging/CountingMessageHandler.h>
using sofa::helper::logging::MainCountingMessageHandler ;

#include <SofaTest/Sofa_test.h>

namespace sofa {


//used to compare lossy images
const float PIXEL_TOLERANCE = 1.28; //0.5% difference on the average of the image

class ImageCImg_test : public ::testing::Test
class ImageCImg_test : public Sofa_test<>
{
protected:
ImageCImg_test() {
MessageDispatcher::clearHandlers() ;
MessageDispatcher::addHandler( &MainCountingMessageHandler::getInstance() ) ;
MessageDispatcher::addHandler( &MainLoggingMessageHandler::getInstance() ) ;

}

void SetUp()
Expand Down Expand Up @@ -144,21 +131,27 @@ class ImageCImg_test : public ::testing::Test

TEST_F(ImageCImg_test, ImageCImg_NoFile)
{
/// This generates a test failure if no error message is generated.
ExpectMessage raii(Message::Error) ;
/// This generates a test failure if no error message is generated.
EXPECT_MSG_EMIT(Error) ;

sofa::helper::io::ImageCImg imgNoFile;
EXPECT_FALSE(imgNoFile.load("randomnamewhichdoesnotexist.png"));

sofa::helper::io::ImageCImg imgNoFile;
EXPECT_FALSE(imgNoFile.load("randomnamewhichdoesnotexist.png"));
}

TEST_F(ImageCImg_test, ImageCImg_NoImg)
{
EXPECT_MSG_EMIT(Error) ;
EXPECT_MSG_NOEMIT(Warning) ;

sofa::helper::io::ImageCImg imgNoImage;
EXPECT_FALSE(imgNoImage.load("imagetest_noimage.png"));
}

TEST_F(ImageCImg_test, ImageCImg_ReadBlackWhite)
{
EXPECT_MSG_NOEMIT(Error, Warning) ;

unsigned int width = 800;
unsigned int height = 600;
unsigned int bpp = 3;//images are RGB
Expand Down Expand Up @@ -195,6 +188,8 @@ TEST_F(ImageCImg_test, ImageCImg_ReadBlackWhite)

TEST_F(ImageCImg_test, ImageCImg_WriteBlackWhite)
{
EXPECT_MSG_NOEMIT(Error, Warning) ;

unsigned int width = 800;
unsigned int height = 600;
unsigned int bpp = 3;//image is RGB
Expand Down
5 changes: 1 addition & 4 deletions applications/plugins/CImgPlugin/ImageCImg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ void ImageCImg::setCimgCreators()

bool ImageCImg::load(std::string filename)
{
cimg_library::cimg::exception_mode(0);

//msg_info("ImageCImg") << "Using CImgPlugin for " << filename;

cimg_library::cimg::exception_mode(0);
m_bLoaded = 0;

if (!sofa::helper::system::DataRepository.findFile(filename))
Expand Down
7 changes: 1 addition & 6 deletions applications/plugins/CImgPlugin/ImageCImg.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ class SOFA_CIMGPLUGIN_API ImageCImgCreators
class SOFA_CIMGPLUGIN_API ImageCImg : public Image
{
public:
ImageCImg ()
{
helper::logging::MessageDispatcher::clearHandlers() ;
helper::logging::MessageDispatcher::addHandler( &helper::logging::MainCountingMessageHandler::getInstance() ) ;
helper::logging::MessageDispatcher::addHandler( &helper::logging::MainLoggingMessageHandler::getInstance() ) ;
}
ImageCImg (){}

ImageCImg (const std::string &filename)
{
Expand Down

0 comments on commit e65ef09

Please sign in to comment.