ctkPluginFrameworkTestMain.cpp 3.5 KB

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