ctkPluginFrameworkTestMain.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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 <QCoreApplication>
  16. #include <QDirIterator>
  17. #include <QTest>
  18. #include <QThread>
  19. #include <QDebug>
  20. #include <ctkPluginContext.h>
  21. #include <ctkPluginConstants.h>
  22. #include <ctkPluginFrameworkFactory.h>
  23. #include <ctkPluginFramework.h>
  24. #include <ctkPluginException.h>
  25. #include <ctkServiceReference.h>
  26. #include "ctkTestSuiteInterface.h"
  27. class TestRunner : public QThread
  28. {
  29. public:
  30. TestRunner(ctkPluginContext* context, long testPluginId, int argc, char** argv)
  31. : context(context), testPluginId(testPluginId), argc(argc), argv(argv)
  32. {
  33. }
  34. void run()
  35. {
  36. // start the main test plugin which registers the test suites (QObject classes)
  37. QSharedPointer<ctkPlugin> fwTest = context->getPlugin(testPluginId);
  38. fwTest->start();
  39. QList<ctkServiceReference> refs = context->getServiceReferences<ctkTestSuiteInterface>();
  40. int result = 0;
  41. foreach(ctkServiceReference ref, refs)
  42. {
  43. result += QTest::qExec(context->getService(ref), argc, argv);
  44. if (result > 0) break;
  45. }
  46. if (result > 0) QCoreApplication::exit(result);
  47. }
  48. private:
  49. ctkPluginContext* context;
  50. long testPluginId;
  51. int argc;
  52. char** argv;
  53. };
  54. int main(int argc, char** argv)
  55. {
  56. QCoreApplication app(argc, argv);
  57. app.setOrganizationName("CTK");
  58. app.setOrganizationDomain("commontk.org");
  59. app.setApplicationName("ctkPluginFrameworkCppTests");
  60. QString pluginDir;
  61. #ifdef CMAKE_INTDIR
  62. pluginDir = qApp->applicationDirPath() + "/../test_plugins/" CMAKE_INTDIR "/";
  63. #else
  64. pluginDir = qApp->applicationDirPath() + "/test_plugins/";
  65. #endif
  66. QApplication::addLibraryPath(pluginDir);
  67. ctkProperties fwProps;
  68. fwProps.insert(ctkPluginConstants::FRAMEWORK_STORAGE_CLEAN, ctkPluginConstants::FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
  69. fwProps.insert("pluginfw.testDir", pluginDir);
  70. ctkPluginFrameworkFactory fwFactory(fwProps);
  71. QSharedPointer<ctkPluginFramework> framework = fwFactory.getFramework();
  72. framework->start();
  73. ctkPluginContext* context = framework->getPluginContext();
  74. long fwTestPluginId = -1;
  75. QStringList libFilter;
  76. libFilter << "*.dll" << "*.so" << "*.dylib";
  77. QDirIterator dirIter(pluginDir, libFilter, QDir::Files);
  78. while(dirIter.hasNext())
  79. {
  80. dirIter.next();
  81. if (dirIter.fileName().contains("org_commontk_pluginfwtest"))
  82. {
  83. try
  84. {
  85. fwTestPluginId = context->installPlugin(QUrl::fromLocalFile(dirIter.filePath()).toString())->getPluginId();
  86. break;
  87. }
  88. catch (const ctkPluginException& e)
  89. {
  90. qCritical() << e.what();
  91. }
  92. }
  93. }
  94. if (fwTestPluginId < 0)
  95. {
  96. qCritical() << "Could not find the plugin framework test plugin: org.commontk.pluginfwtest";
  97. }
  98. // QList<ctkServiceReference> refs = context->getServiceReferences("ctkTestSuiteInterface");
  99. // int result = 0;
  100. // foreach(ctkServiceReference ref, refs)
  101. // {
  102. // result = QTest::qExec(context->getService(ref), argc, argv);
  103. // }
  104. // return result;
  105. TestRunner runner(context, fwTestPluginId, argc, argv);
  106. runner.connect(&runner, SIGNAL(finished()), &app, SLOT(quit()));
  107. runner.start();
  108. return app.exec();
  109. }