ctkErrorLogModelTerminalOutputTest1.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #include <QProcess>
  18. // CTK includes
  19. #include "ctkCommandLineParser.h"
  20. #include "ctkErrorLogFDMessageHandler.h"
  21. #include "ctkErrorLogQtMessageHandler.h"
  22. #include "ctkErrorLogStreamMessageHandler.h"
  23. #include "ctkModelTester.h"
  24. // STL includes
  25. #include <cstdio>
  26. #include <cstdlib>
  27. #include <iostream>
  28. // Helper functions
  29. #include "Testing/Cpp/ctkErrorLogModelTestHelper.cpp"
  30. namespace
  31. {
  32. //-----------------------------------------------------------------------------
  33. bool checkTerminalOutput(const QStringList& expectedMessages)
  34. {
  35. ctkCommandLineParser parser;
  36. parser.setArgumentPrefix("--", "-");
  37. parser.addArgument("test-launcher", "", QVariant::String, "Path to test launcher");
  38. bool ok = false;
  39. QHash<QString, QVariant> parsedArgs = parser.parseArguments(QCoreApplication::arguments(), &ok);
  40. if(!ok)
  41. {
  42. std::cerr << "Line " << __LINE__ << " - Failed to parse arguments !" << std::endl;
  43. return false;
  44. }
  45. if (parsedArgs.contains("test-launcher"))
  46. {
  47. QString testLauncher = parsedArgs.value("test-launcher").toString();
  48. if (!QFile::exists(testLauncher))
  49. {
  50. std::cerr << "Line " << __LINE__ << " - Couldn't find test launcher !\n"
  51. << "\ttest-launcher:" << qPrintable(testLauncher) << std::endl;
  52. return false;
  53. }
  54. QProcess process;
  55. process.setProcessChannelMode(QProcess::MergedChannels);
  56. process.start(testLauncher, QStringList() << QCoreApplication::arguments().at(0));
  57. process.waitForFinished(1000);
  58. QString output = process.readAll();
  59. QString errorMsg = checkTextMessages(__LINE__, output.split("\n"), expectedMessages);
  60. if (!errorMsg.isEmpty())
  61. {
  62. printErrorMessage(errorMsg);
  63. return false;
  64. }
  65. process.waitForFinished(1000);
  66. }
  67. return true;
  68. }
  69. }
  70. //-----------------------------------------------------------------------------
  71. int ctkErrorLogModelTerminalOutputTest1(int argc, char * argv [])
  72. {
  73. QCoreApplication app(argc, argv);
  74. Q_UNUSED(app);
  75. QString fdMessage0("This is stdout message");
  76. QString fdMessage1("This is stderr message");
  77. QString qtMessage0("This is a qDebug message");
  78. QString qtMessage1("This is a qWarning message");
  79. QString qtMessage2("This is a qCritical message");
  80. QString stdMessage0("This is a std::cerr message");
  81. QString stdMessage1("This is a std::cout message");
  82. QStringList expectedMessages;
  83. expectedMessages << fdMessage0 << fdMessage1
  84. << qtMessage0 << qtMessage1 << qtMessage2
  85. << stdMessage0 << stdMessage1;
  86. // Since the order of the messages outputed on the terminal is not deterministic,
  87. // let's just make sure that all messages have been displayed on the terminal
  88. // independently of their order.
  89. if (!checkTerminalOutput(expectedMessages))
  90. {
  91. return EXIT_FAILURE;
  92. }
  93. QString errorMsg;
  94. ctkErrorLogModel model;
  95. ctkErrorLogTerminalOutput::TerminalOutputs currentTerminalOutputEnabled = model.terminalOutputs();
  96. errorMsg = checkBoolean(__LINE__, "TerminalOutputEnabled",
  97. currentTerminalOutputEnabled, ctkErrorLogTerminalOutput::None);
  98. if (!errorMsg.isEmpty())
  99. {
  100. model.disableAllMsgHandler();
  101. printErrorMessage(errorMsg);
  102. printTextMessages(model);
  103. return EXIT_FAILURE;
  104. }
  105. model.setTerminalOutputs(ctkErrorLogTerminalOutput::All);
  106. currentTerminalOutputEnabled = model.terminalOutputs();
  107. errorMsg = checkBoolean(__LINE__, "TerminalOutputEnabled",
  108. currentTerminalOutputEnabled, ctkErrorLogTerminalOutput::All);
  109. if (!errorMsg.isEmpty())
  110. {
  111. model.disableAllMsgHandler();
  112. printErrorMessage(errorMsg);
  113. printTextMessages(model);
  114. return EXIT_FAILURE;
  115. }
  116. ctkModelTester modelTester;
  117. modelTester.setVerbose(false);
  118. try
  119. {
  120. modelTester.setModel(&model);
  121. // Monitor Qt messages
  122. model.registerMsgHandler(new ctkErrorLogQtMessageHandler);
  123. model.setMsgHandlerEnabled(ctkErrorLogQtMessageHandler::HandlerName, true);
  124. // Monitor Stream messages
  125. model.registerMsgHandler(new ctkErrorLogStreamMessageHandler);
  126. model.setMsgHandlerEnabled(ctkErrorLogStreamMessageHandler::HandlerName, true);
  127. // Monitor FD messages
  128. model.registerMsgHandler(new ctkErrorLogFDMessageHandler);
  129. model.setMsgHandlerEnabled(ctkErrorLogFDMessageHandler::HandlerName, true);
  130. fprintf(stdout, "%s\n", qPrintable(fdMessage0));
  131. fflush(stdout);
  132. qDebug().nospace() << qPrintable(qtMessage0);
  133. std::cerr << qPrintable(stdMessage0) << std::endl;
  134. qWarning().nospace() << qPrintable(qtMessage1);
  135. fprintf(stderr, "%s\n", qPrintable(fdMessage1));
  136. fflush(stderr);
  137. std::cout << qPrintable(stdMessage1) << std::endl;
  138. qCritical().nospace() << qPrintable(qtMessage2);
  139. // Give enough time to the ErrorLogModel to consider the queued messages.
  140. processEvents(1000);
  141. errorMsg = checkRowCount(__LINE__, model.rowCount(), /* expected = */ expectedMessages.count());
  142. if (!errorMsg.isEmpty())
  143. {
  144. model.disableAllMsgHandler();
  145. printErrorMessage(errorMsg);
  146. printTextMessages(model);
  147. return EXIT_FAILURE;
  148. }
  149. errorMsg = checkTextMessages(__LINE__, model, expectedMessages);
  150. if (!errorMsg.isEmpty())
  151. {
  152. model.disableAllMsgHandler();
  153. printErrorMessage(errorMsg);
  154. printTextMessages(model);
  155. return EXIT_FAILURE;
  156. }
  157. }
  158. catch (const char* error)
  159. {
  160. model.disableAllMsgHandler();
  161. std::cerr << error << std::endl;
  162. return EXIT_FAILURE;
  163. }
  164. return EXIT_SUCCESS;
  165. }