ctkErrorLogModelTest2.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. } // end namespace
  76. //-----------------------------------------------------------------------------
  77. int ctkErrorLogModelTest2(int argc, char * argv [])
  78. {
  79. QCoreApplication app(argc, argv);
  80. Q_UNUSED(app);
  81. ctkErrorLogModel model;
  82. ctkModelTester modelTester;
  83. modelTester.setVerbose(false);
  84. QString errorMsg;
  85. try
  86. {
  87. modelTester.setModel(&model);
  88. // --------------------------------------------------------------------------
  89. // Monitor Qt messages
  90. model.registerMsgHandler(new ctkErrorLogQtMessageHandler);
  91. model.setMsgHandlerEnabled(ctkErrorLogQtMessageHandler::HandlerName, true);
  92. // --------------------------------------------------------------------------
  93. // Test LogEntryGrouping
  94. model.setLogEntryGrouping(true);
  95. QString qtMessage0("This is a qDebug message - 1");
  96. qDebug().nospace() << qPrintable(qtMessage0);
  97. QString qtMessage0b("This is a qDebug message - 2");
  98. qDebug().nospace() << qPrintable(qtMessage0b);
  99. QString qtMessage1("This is a qWarning message");
  100. qWarning().nospace() << qPrintable(qtMessage1);
  101. QString qtMessage2("This is a qCritical message - 1");
  102. qCritical().nospace() << qPrintable(qtMessage2);
  103. QString qtMessage2b("This is a qCritical message - 2");
  104. qCritical().nospace() << qPrintable(qtMessage2b);
  105. QStringList expectedQtMessages;
  106. expectedQtMessages << qtMessage0.append("\n").append(qtMessage0b)
  107. << qtMessage1
  108. << qtMessage2.append("\n").append(qtMessage2b);
  109. errorMsg = checkRowCount(__LINE__, model.rowCount(), /* expected = */ expectedQtMessages.count());
  110. if (!errorMsg.isEmpty())
  111. {
  112. model.disableAllMsgHandler();
  113. printErrorMessage(errorMsg);
  114. printTextMessages(model);
  115. return EXIT_FAILURE;
  116. }
  117. errorMsg = checkTextMessages(__LINE__, model, expectedQtMessages);
  118. if (!errorMsg.isEmpty())
  119. {
  120. model.disableAllMsgHandler();
  121. printErrorMessage(errorMsg);
  122. return EXIT_FAILURE;
  123. }
  124. }
  125. catch (const char* error)
  126. {
  127. model.disableAllMsgHandler();
  128. std::cerr << error << std::endl;
  129. return EXIT_FAILURE;
  130. }
  131. return EXIT_SUCCESS;
  132. }