ctkAbstractPluginFactoryTest1.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <QTimer>
  17. // CTK includes
  18. #include "ctkAbstractPluginFactory.h"
  19. #include "ctkDummyPlugin.h"
  20. // STD includes
  21. #include <cstdlib>
  22. #include <iostream>
  23. //-----------------------------------------------------------------------------
  24. int ctkAbstractPluginFactoryTest1(int argc, char * argv [])
  25. {
  26. QCoreApplication app(argc, argv);
  27. if (argc <= 1)
  28. {
  29. std::cerr << "Missing argument" << std::endl;
  30. return EXIT_FAILURE;
  31. }
  32. QString filePath(argv[1]);
  33. QFileInfo file(filePath);
  34. while (filePath.contains("$(OutDir)"))
  35. {
  36. QString debugFilePath = filePath;
  37. debugFilePath.replace("$(OutDir)","Debug");
  38. if (QFile::exists(QString(debugFilePath)))
  39. {
  40. file = QFileInfo(debugFilePath);
  41. break;
  42. }
  43. QString releaseFilePath = filePath;
  44. releaseFilePath.replace("$(OutDir)","Release");
  45. if (QFile::exists(QString(releaseFilePath)))
  46. {
  47. file = QFileInfo(releaseFilePath);
  48. break;
  49. }
  50. return EXIT_FAILURE;
  51. }
  52. ctkAbstractPluginFactory< ctkDummyPlugin > pluginFactory;
  53. pluginFactory.setVerbose(true);
  54. QString itemKey = pluginFactory.registerFileItem(QFileInfo("foo/bar.txt"));
  55. if (!itemKey.isEmpty())
  56. {
  57. std::cerr << "ctkAbstractPluginFactory::registerLibrary() registered bad file"
  58. << std::endl;
  59. return EXIT_FAILURE;
  60. }
  61. itemKey = pluginFactory.registerFileItem(file);
  62. if (itemKey.isEmpty() || pluginFactory.itemKeys().count() != 1)
  63. {
  64. std::cerr << __LINE__ << ": ctkAbstractPluginFactory::registerLibrary() failed: "
  65. << pluginFactory.itemKeys().count() << std::endl;
  66. return EXIT_FAILURE;
  67. }
  68. // register twice must return false
  69. itemKey = pluginFactory.registerFileItem(file);
  70. if (itemKey.isEmpty() || pluginFactory.itemKeys().count() != 1)
  71. {
  72. std::cerr << __LINE__ << ": ctkAbstractPluginFactory::registerLibrary() failed: "
  73. << qPrintable(itemKey) << " count: "
  74. << pluginFactory.itemKeys().count() << std::endl;
  75. return EXIT_FAILURE;
  76. }
  77. if (QFileInfo(pluginFactory.path(itemKey)) != file)
  78. {
  79. std::cerr << __LINE__ << ": ctkAbstractPluginFactory::registerLibrary() failed: "
  80. << pluginFactory.path(itemKey).toStdString() << std::endl;
  81. return EXIT_FAILURE;
  82. }
  83. ctkDummyPlugin* plugin = pluginFactory.instantiate(itemKey);
  84. if (plugin == 0)
  85. {
  86. std::cerr << __LINE__ << ": ctkAbstractPluginFactory::instantiate() failed" << std::endl;
  87. return EXIT_FAILURE;
  88. }
  89. pluginFactory.uninstantiate(itemKey);
  90. // ctkDummyPlugin is not a QPushButton, it should fail then.
  91. ctkAbstractPluginFactory< QTimer > buttonPluginFactory;
  92. buttonPluginFactory.setVerbose(true);
  93. // it should register but fail while instanciating
  94. itemKey = buttonPluginFactory.registerFileItem(file);
  95. if (itemKey.isEmpty() || buttonPluginFactory.itemKeys().count() != 1)
  96. {
  97. std::cerr << __LINE__ << ": ctkAbstractPluginFactory::registerLibrary() failed" << std::endl;
  98. return EXIT_FAILURE;
  99. }
  100. QTimer* timer = buttonPluginFactory.instantiate("foo");
  101. if (timer != 0)
  102. {
  103. std::cerr << __LINE__ << ": ctkAbstractPluginFactory::instantiate() failed" << std::endl;
  104. return EXIT_FAILURE;
  105. }
  106. return EXIT_SUCCESS;
  107. }