ctkErrorLogModelEntryGroupingTest1.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.apache.org/licenses/LICENSE-2.0.txt
  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. // CTK includes
  18. #include "ctkErrorLogQtMessageHandler.h"
  19. #include "ctkModelTester.h"
  20. // STL includes
  21. #include <cstdlib>
  22. #include <iostream>
  23. // Helper functions
  24. #include "Testing/Cpp/ctkErrorLogModelTestHelper.cpp"
  25. //-----------------------------------------------------------------------------
  26. int ctkErrorLogModelEntryGroupingTest1(int argc, char * argv [])
  27. {
  28. QCoreApplication app(argc, argv);
  29. Q_UNUSED(app);
  30. ctkErrorLogModel model;
  31. ctkModelTester modelTester;
  32. modelTester.setVerbose(false);
  33. QString errorMsg;
  34. try
  35. {
  36. modelTester.setModel(&model);
  37. // --------------------------------------------------------------------------
  38. // Monitor Qt messages
  39. model.registerMsgHandler(new ctkErrorLogQtMessageHandler);
  40. model.setMsgHandlerEnabled(ctkErrorLogQtMessageHandler::HandlerName, true);
  41. // --------------------------------------------------------------------------
  42. // Test LogEntryGrouping
  43. model.setLogEntryGrouping(true);
  44. QString qtMessage0("This is a qDebug message - 1");
  45. qDebug().nospace() << qPrintable(qtMessage0);
  46. QString qtMessage0b("This is a qDebug message - 2");
  47. qDebug().nospace() << qPrintable(qtMessage0b);
  48. QString qtMessage1("This is a qWarning message");
  49. qWarning().nospace() << qPrintable(qtMessage1);
  50. QString qtMessage2("This is a qCritical message - 1");
  51. qCritical().nospace() << qPrintable(qtMessage2);
  52. QString qtMessage2b("This is a qCritical message - 2");
  53. qCritical().nospace() << qPrintable(qtMessage2b);
  54. // Give enough time to the ErrorLogModel to consider the queued messages.
  55. processEvents(1000);
  56. QStringList expectedQtMessages;
  57. expectedQtMessages << qtMessage0.append("\n").append(qtMessage0b)
  58. << qtMessage1
  59. << qtMessage2.append("\n").append(qtMessage2b);
  60. errorMsg = checkRowCount(__LINE__, model.rowCount(), /* expected = */ expectedQtMessages.count());
  61. if (!errorMsg.isEmpty())
  62. {
  63. model.disableAllMsgHandler();
  64. printErrorMessage(errorMsg);
  65. printTextMessages(model);
  66. return EXIT_FAILURE;
  67. }
  68. errorMsg = checkTextMessages(__LINE__, model, expectedQtMessages);
  69. if (!errorMsg.isEmpty())
  70. {
  71. model.disableAllMsgHandler();
  72. printErrorMessage(errorMsg);
  73. printTextMessages(model);
  74. return EXIT_FAILURE;
  75. }
  76. }
  77. catch (const char* error)
  78. {
  79. model.disableAllMsgHandler();
  80. std::cerr << error << std::endl;
  81. return EXIT_FAILURE;
  82. }
  83. return EXIT_SUCCESS;
  84. }