UnitTestEmittingStringBuffer.cxx 2.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//=========================================================================
//  Copyright (c) Kitware, Inc.
//  All rights reserved.
//  See LICENSE.txt for details.
//
//  This software is distributed WITHOUT ANY WARRANTY; without even
//  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
//  PURPOSE.  See the above copyright notice for more information.
//=========================================================================

// .NAME UnitTestEmittingStringBuffer.h -
// .SECTION Description
// .SECTION See Also

15
#include "smtk/extension/qt/testing/cxx/UnitTestEmittingStringBuffer.h"
16 17 18 19
#include "smtk/extension/qt/qtEmittingStringBuffer.h"

#include "smtk/extension/qt/testing/cxx/qtPrintLog.h"

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include <QCoreApplication>
#include <QTime>
#include <QTimer>

namespace
{
void flushEventQueue()
{
  QTime dieTime = QTime::currentTime().addMSecs(1);
  while (QTime::currentTime() < dieTime)
  {
    QCoreApplication::processEvents(QEventLoop::AllEvents, 1);
  }
}
}

void TestEmittingStringBuffer::run()
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
{
  // Create an instance of Logger
  smtk::io::Logger logger;

  // Create an instance of our emitting string buffer
  smtk::extension::qtEmittingStringBuffer stringbuf;

  // Create a new std::ostream that uses our string buffer
  std::ostream* ostr = new std::ostream(&stringbuf);

  // Pass the ostream to the logger, and set it to be owned by the logger
  logger.setFlushToStream(ostr, true, false);

  // Create a PrintLogInstance to connect with our EmittingStringBuffer
  qtPrintLog printLog(logger);
52
  QObject::connect(&stringbuf, SIGNAL(flush()), &printLog, SLOT(print()), Qt::QueuedConnection);
53 54 55

  // Test the logger.
  smtkErrorMacro(logger, "this is an error no = " << 45 << " ERROR!");
56
  flushEventQueue();
57
  smtkWarningMacro(logger, "this is a warning no = " << 10.1234 << " WARNING!");
58
  flushEventQueue();
59
  smtkDebugMacro(logger, "this is a Debug no = " << 1 << " DEBUG!");
60
  flushEventQueue();
61
  logger.addRecord(smtk::io::Logger::INFO, "Sample Info String\n");
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  flushEventQueue();
  emit finished();
}

int UnitTestEmittingStringBuffer(int argc, char** const argv)
{
  QCoreApplication application(argc, argv);

  // Create an instance of our test harness
  TestEmittingStringBuffer* testEmittingStringBuffer = new TestEmittingStringBuffer(&application);

  // Exit when the task signals finished
  QObject::connect(testEmittingStringBuffer, &TestEmittingStringBuffer::finished,
    [&application]() { QTimer::singleShot(0, &application, &QCoreApplication::quit); });

  // Run the task from the application event loop
  QTimer::singleShot(0, testEmittingStringBuffer, SLOT(run()));

  return application.exec();
81 82 83

  return 0;
}