ctkCmdLineModuleFutureTest.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include <ctkCmdLineModuleManager.h>
  16. #include <ctkCmdLineModuleFactory.h>
  17. #include <ctkCmdLineModule.h>
  18. #include <ctkCmdLineModuleReference.h>
  19. #include <ctkCmdLineModuleDescription.h>
  20. #include <ctkCmdLineModuleParameter.h>
  21. #include <ctkCmdLineModuleXmlProgressWatcher.h>
  22. #include <QBuffer>
  23. #include "ctkCmdLineModuleSignalTester.h"
  24. #include <QVariant>
  25. #include <QCoreApplication>
  26. #include <QDebug>
  27. #include <QFuture>
  28. #include <QFutureWatcher>
  29. #include <cstdlib>
  30. class ctkCmdLineModuleTestInstanceFactory : public ctkCmdLineModuleFactory
  31. {
  32. public:
  33. virtual ctkCmdLineModule* create(const ctkCmdLineModuleReference& moduleRef)
  34. {
  35. struct ModuleTestInstance : public ctkCmdLineModule
  36. {
  37. ModuleTestInstance(const ctkCmdLineModuleReference& moduleRef) : ctkCmdLineModule(moduleRef) {}
  38. virtual QObject* guiHandle() const { return NULL; }
  39. virtual QVariant value(const QString& parameter) const
  40. {
  41. return this->moduleReference().description().parameter(parameter).defaultValue();
  42. }
  43. virtual void setValue(const QString& /*parameter*/, const QVariant& /*value*/)
  44. {
  45. // do nothing
  46. }
  47. };
  48. return new ModuleTestInstance(moduleRef);
  49. }
  50. };
  51. int ctkCmdLineModuleFutureTest(int argc, char* argv[])
  52. {
  53. QCoreApplication app(argc, argv);
  54. ctkCmdLineModuleTestInstanceFactory factory;
  55. ctkCmdLineModuleManager manager(&factory);
  56. QString moduleFilename = app.applicationDirPath() + "/ctkCmdLineModuleTestBed";
  57. ctkCmdLineModuleReference moduleRef = manager.registerModule(moduleFilename);
  58. if (!moduleRef)
  59. {
  60. qCritical() << "Module at" << moduleFilename << "could not be registered";
  61. }
  62. ctkCmdLineModule* module = manager.createModule(moduleRef);
  63. QList<QString> expectedSignals;
  64. expectedSignals.push_back("module.started");
  65. expectedSignals.push_back("module.finished");
  66. ctkCmdLineModuleSignalTester signalTester;
  67. QFutureWatcher<QString> watcher;
  68. QObject::connect(&watcher, SIGNAL(started()), &signalTester, SLOT(moduleStarted()));
  69. QObject::connect(&watcher, SIGNAL(finished()), &signalTester, SLOT(moduleFinished()));
  70. QFuture<QString> future = module->run();
  71. watcher.setFuture(future);
  72. future.waitForFinished();
  73. // process pending events
  74. QCoreApplication::processEvents();
  75. delete module;
  76. if (!signalTester.checkSignals(expectedSignals))
  77. {
  78. return EXIT_FAILURE;
  79. }
  80. return EXIT_SUCCESS;
  81. }