ctkErrorLogModelTest3.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QCoreApplication>
  16. #include <QDebug>
  17. #include <QStringList>
  18. // CTK includes
  19. #include "ctkErrorLogModel.h"
  20. #include "ctkErrorLogQtMessageHandler.h"
  21. #include "ctkModelTester.h"
  22. // STL includes
  23. #include <cstdlib>
  24. #include <iostream>
  25. namespace
  26. {
  27. //-----------------------------------------------------------------------------
  28. // Utility function
  29. //-----------------------------------------------------------------------------
  30. QString checkRowCount(int line, int currentRowCount, int expectedRowCount)
  31. {
  32. if (currentRowCount != expectedRowCount)
  33. {
  34. QString errorMsg("Line %1 - Expected rowCount: %2 - Current rowCount: %3\n");
  35. return errorMsg.arg(line).arg(expectedRowCount).arg(currentRowCount);
  36. }
  37. return QString();
  38. }
  39. //-----------------------------------------------------------------------------
  40. QString checkTextMessages(int line, const ctkErrorLogModel& model, const QStringList& expectedMessages)
  41. {
  42. for(int i=0; i < expectedMessages.count(); ++i)
  43. {
  44. QModelIndex descriptionIndex = model.index(i, ctkErrorLogModel::DescriptionColumn);
  45. QString currentMessage = descriptionIndex.data(ctkErrorLogModel::DescriptionTextRole).toString();
  46. if (currentMessage.compare(expectedMessages.value(i)) != 0)
  47. {
  48. QString errorMsg("Line %1 - Problem with row%2 !\n"
  49. "\tExpected message [%3]\n"
  50. "\tCurrent message [%4]\n");
  51. return errorMsg.arg(line).arg(i).arg(expectedMessages.value(i)).arg(currentMessage);
  52. }
  53. }
  54. return QString();
  55. }
  56. //-----------------------------------------------------------------------------
  57. void printTextMessages(const ctkErrorLogModel& model)
  58. {
  59. fprintf(stdout, "%s", "ErrorLogModel rows:\n");
  60. QString text("\trow %1 => %2\n");
  61. for (int i=0; i < model.rowCount(); ++i)
  62. {
  63. QString description =
  64. model.index(0, ctkErrorLogModel::DescriptionColumn).data().toString();
  65. fprintf(stdout, "%s", qPrintable(text.arg(i).arg(description)));
  66. }
  67. fflush(stdout);
  68. }
  69. //-----------------------------------------------------------------------------
  70. void printErrorMessage(const QString& errorMessage)
  71. {
  72. fprintf(stderr, "%s", qPrintable(errorMessage));
  73. fflush(stderr);
  74. }
  75. //-----------------------------------------------------------------------------
  76. QString checkBoolean(int line, const char* valueName, bool current, bool expected)
  77. {
  78. if (current != expected)
  79. {
  80. QString errorMsg("Line %1 - Expected %2: %3 - Current %4: %5\n");
  81. return errorMsg.arg(line).arg(valueName).
  82. arg(static_cast<int>(expected)).arg(valueName).arg(static_cast<int>(current));
  83. }
  84. return QString();
  85. }
  86. } // end namespace
  87. //-----------------------------------------------------------------------------
  88. int ctkErrorLogModelTest3(int argc, char * argv [])
  89. {
  90. QCoreApplication app(argc, argv);
  91. Q_UNUSED(app);
  92. QString errorMsg;
  93. ctkErrorLogModel model;
  94. bool currentTerminalOutputEnabled = model.terminalOutputEnabled();
  95. errorMsg = checkBoolean(__LINE__, "TerminalOutputEnabled", currentTerminalOutputEnabled, false);
  96. if (!errorMsg.isEmpty())
  97. {
  98. model.disableAllMsgHandler();
  99. printErrorMessage(errorMsg);
  100. printTextMessages(model);
  101. return EXIT_FAILURE;
  102. }
  103. model.setTerminalOutputEnabled(true);
  104. currentTerminalOutputEnabled = model.terminalOutputEnabled();
  105. errorMsg = checkBoolean(__LINE__, "TerminalOutputEnabled", currentTerminalOutputEnabled, true);
  106. if (!errorMsg.isEmpty())
  107. {
  108. model.disableAllMsgHandler();
  109. printErrorMessage(errorMsg);
  110. printTextMessages(model);
  111. return EXIT_FAILURE;
  112. }
  113. ctkModelTester modelTester;
  114. modelTester.setVerbose(false);
  115. try
  116. {
  117. modelTester.setModel(&model);
  118. // --------------------------------------------------------------------------
  119. // Monitor Qt messages
  120. model.registerMsgHandler(new ctkErrorLogQtMessageHandler);
  121. model.setMsgHandlerEnabled(ctkErrorLogQtMessageHandler::HandlerName, true);
  122. QString qtMessage0("This is a qDebug message");
  123. qDebug().nospace() << qPrintable(qtMessage0);
  124. QString qtMessage1("This is a qWarning message");
  125. qWarning().nospace() << qPrintable(qtMessage1);
  126. QString qtMessage2("This is a qCritical message");
  127. qCritical().nospace() << qPrintable(qtMessage2);
  128. QStringList expectedQtMessages;
  129. expectedQtMessages << qtMessage0
  130. << qtMessage1
  131. << qtMessage2;
  132. errorMsg = checkRowCount(__LINE__, model.rowCount(), /* expected = */ expectedQtMessages.count());
  133. if (!errorMsg.isEmpty())
  134. {
  135. model.disableAllMsgHandler();
  136. printErrorMessage(errorMsg);
  137. printTextMessages(model);
  138. return EXIT_FAILURE;
  139. }
  140. errorMsg = checkTextMessages(__LINE__, model, expectedQtMessages);
  141. if (!errorMsg.isEmpty())
  142. {
  143. model.disableAllMsgHandler();
  144. printErrorMessage(errorMsg);
  145. return EXIT_FAILURE;
  146. }
  147. }
  148. catch (const char* error)
  149. {
  150. model.disableAllMsgHandler();
  151. std::cerr << error << std::endl;
  152. return EXIT_FAILURE;
  153. }
  154. return EXIT_SUCCESS;
  155. }