ctkPluginFrameworkTestMain.cpp 3.8 KB

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