ctkVTKErrorLogModelTest1.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "ctkVTKErrorLogMessageHandler.h"
  19. #include "ctkModelTester.h"
  20. // VTK includes
  21. #include <vtkOutputWindow.h>
  22. // STL includesQList
  23. #include <cstdlib>
  24. #include <iostream>
  25. // Helper functions
  26. #include "Testing/Cpp/ctkErrorLogModelTestHelper.cpp"
  27. //-----------------------------------------------------------------------------
  28. int ctkVTKErrorLogModelTest1(int argc, char * argv [])
  29. {
  30. QCoreApplication app(argc, argv);
  31. Q_UNUSED(app);
  32. ctkErrorLogModel model;
  33. ctkModelTester modelTester;
  34. modelTester.setVerbose(false);
  35. QString errorMsg;
  36. try
  37. {
  38. modelTester.setModel(&model);
  39. // --------------------------------------------------------------------------
  40. // Monitor VTK messages
  41. model.registerMsgHandler(new ctkVTKErrorLogMessageHandler);
  42. model.setMsgHandlerEnabled(ctkVTKErrorLogMessageHandler::HandlerName, true);
  43. errorMsg = checkRowCount(__LINE__, model.rowCount(), /* expected = */ 0);
  44. if (!errorMsg.isEmpty())
  45. {
  46. model.disableAllMsgHandler();
  47. printErrorMessage(errorMsg);
  48. printTextMessages(model);
  49. return EXIT_FAILURE;
  50. }
  51. QString vtkMessage0("This is a VTK debug message");
  52. vtkOutputWindowDisplayDebugText(qPrintable(vtkMessage0));
  53. QString vtkMessage1("This is a VTK warning message");
  54. vtkOutputWindowDisplayWarningText(qPrintable(vtkMessage1));
  55. QString vtkMessage2("This is a VTK error message");
  56. vtkOutputWindowDisplayErrorText(qPrintable(vtkMessage2));
  57. // Give enough time to the ErrorLogModel to consider the queued messages.
  58. processEvents(1000);
  59. QStringList expectedVTKMessages;
  60. expectedVTKMessages << vtkMessage0 << vtkMessage1 << vtkMessage2;
  61. errorMsg = checkRowCount(__LINE__, model.rowCount(), /* expected = */ expectedVTKMessages.count());
  62. if (!errorMsg.isEmpty())
  63. {
  64. model.disableAllMsgHandler();
  65. printErrorMessage(errorMsg);
  66. return EXIT_FAILURE;
  67. }
  68. errorMsg = checkTextMessages(__LINE__, model, expectedVTKMessages);
  69. if (!errorMsg.isEmpty())
  70. {
  71. model.disableAllMsgHandler();
  72. printErrorMessage(errorMsg);
  73. return EXIT_FAILURE;
  74. }
  75. // Check if handler can be enabled / disabled multiple times in a row
  76. for (int idx = 0; idx < 3; ++idx)
  77. {
  78. model.setMsgHandlerEnabled(ctkVTKErrorLogMessageHandler::HandlerName, false);
  79. model.setMsgHandlerEnabled(ctkVTKErrorLogMessageHandler::HandlerName, true);
  80. }
  81. // Clear
  82. model.clear();
  83. // Disable VTK messages monitoring
  84. model.setMsgHandlerEnabled(ctkVTKErrorLogMessageHandler::HandlerName, false);
  85. vtkOutputWindowDisplayDebugText("This VTK debug message should appear in the console");
  86. vtkOutputWindowDisplayWarningText("This VTK warning message should appear in the console");
  87. vtkOutputWindowDisplayErrorText("This VTK error message should appear in the console");
  88. errorMsg = checkRowCount(__LINE__, model.rowCount(), /* expected = */ 0);
  89. if (!errorMsg.isEmpty())
  90. {
  91. model.disableAllMsgHandler();
  92. printErrorMessage(errorMsg);
  93. printTextMessages(model);
  94. return EXIT_FAILURE;
  95. }
  96. }
  97. catch (const char* error)
  98. {
  99. model.disableAllMsgHandler();
  100. std::cerr << error << std::endl;
  101. return EXIT_FAILURE;
  102. }
  103. return EXIT_SUCCESS;
  104. }