ctkCmdLineModuleFrontendQtGuiTest.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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.txt
  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. // Qt includes
  16. #include <QSpinBox>
  17. #include <QComboBox>
  18. #include <QVariant>
  19. #if (QT_VERSION < QT_VERSION_CHECK(4,7,0))
  20. Q_DECLARE_METATYPE(QVariant)
  21. #endif
  22. // CTK includes
  23. #include "ctkCmdLineModuleManager.h"
  24. #include "ctkCmdLineModuleBackend.h"
  25. #include "ctkCmdLineModuleFrontendQtGui.h"
  26. #include "ctkCmdLineModuleFuture.h"
  27. #include "ctkCmdLineModuleDescription.h"
  28. #include "ctkCmdLineModuleParameter.h"
  29. #include "ctkTest.h"
  30. #if (QT_VERSION < QT_VERSION_CHECK(4,7,0))
  31. extern int qHash(const QUrl& url);
  32. #endif
  33. namespace {
  34. class BackendMockUp : public ctkCmdLineModuleBackend
  35. {
  36. public:
  37. void addModule(const QUrl& location, const QByteArray& xml)
  38. {
  39. this->UrlToXml[location] = xml;
  40. }
  41. virtual QString name() const { return "Mockup"; }
  42. virtual QString description() const { return "Test Mock-up"; }
  43. virtual QList<QString> schemes() const { return QList<QString>() << "test"; }
  44. virtual qint64 timeStamp(const QUrl& /*location*/) const { return 0; }
  45. virtual QByteArray rawXmlDescription(const QUrl& location, int /*timeout*/)
  46. {
  47. return UrlToXml[location];
  48. }
  49. protected:
  50. virtual ctkCmdLineModuleFuture run(ctkCmdLineModuleFrontend* /*frontend*/)
  51. {
  52. return ctkCmdLineModuleFuture();
  53. }
  54. private:
  55. QHash<QUrl, QByteArray> UrlToXml;
  56. };
  57. }
  58. // ----------------------------------------------------------------------------
  59. class ctkCmdLineModuleFrontendQtGuiTester: public QObject
  60. {
  61. Q_OBJECT
  62. private:
  63. QScopedPointer<ctkCmdLineModuleBackend> Backend;
  64. ctkCmdLineModuleManager Manager;
  65. ctkCmdLineModuleReference ModuleRef;
  66. QString ChangedParameter;
  67. QVariant ChangedParameterValue;
  68. private Q_SLOTS:
  69. void valueChanged(const QString& parameter, const QVariant& value);
  70. private Q_SLOTS:
  71. void initTestCase();
  72. void init();
  73. void testValueSetterAndGetter();
  74. void testValueSetterAndGetter_data();
  75. };
  76. // ----------------------------------------------------------------------------
  77. void ctkCmdLineModuleFrontendQtGuiTester::valueChanged(const QString &parameter, const QVariant &value)
  78. {
  79. this->ChangedParameter = parameter;
  80. this->ChangedParameterValue = value;
  81. }
  82. // ----------------------------------------------------------------------------
  83. void ctkCmdLineModuleFrontendQtGuiTester::initTestCase()
  84. {
  85. BackendMockUp* backend = new BackendMockUp;
  86. this->Backend.reset(backend);
  87. QFile xmlFile(":/ctkCmdLineModuleFrontendQtGuiTestModule1.xml");
  88. QVERIFY(xmlFile.open(QIODevice::ReadOnly));
  89. backend->addModule(QUrl("test://module1"), xmlFile.readAll());
  90. this->Manager.registerBackend(backend);
  91. this->ModuleRef = this->Manager.registerModule(QUrl("test://module1"));
  92. QVERIFY(this->ModuleRef);
  93. }
  94. // ----------------------------------------------------------------------------
  95. void ctkCmdLineModuleFrontendQtGuiTester::init()
  96. {
  97. this->ChangedParameter.clear();
  98. this->ChangedParameterValue.clear();
  99. }
  100. // ----------------------------------------------------------------------------
  101. void ctkCmdLineModuleFrontendQtGuiTester::testValueSetterAndGetter()
  102. {
  103. QScopedPointer<ctkCmdLineModuleFrontend> frontend(new ctkCmdLineModuleFrontendQtGui(this->ModuleRef));
  104. // force the creation of the frontend gui
  105. frontend->guiHandle();
  106. connect(frontend.data(), SIGNAL(valueChanged(QString,QVariant)), SLOT(valueChanged(QString,QVariant)));
  107. QFETCH(QString, parameter);
  108. QFETCH(QVariant, currentValue);
  109. QFETCH(QVariant, newValue);
  110. QFETCH(QVariant, expectedValue);
  111. QFETCH(int, role);
  112. if (role == -1)
  113. {
  114. // test with default role argument
  115. QCOMPARE(frontend->value(parameter), currentValue);
  116. }
  117. else
  118. {
  119. QCOMPARE(frontend->value(parameter, role), currentValue);
  120. }
  121. // test setting values
  122. if (newValue.isValid())
  123. {
  124. frontend->setValue(parameter, newValue);
  125. if (role == -1)
  126. {
  127. QCOMPARE(frontend->value(parameter), expectedValue);
  128. }
  129. else
  130. {
  131. QCOMPARE(frontend->value(parameter, role), expectedValue);
  132. if (role == ctkCmdLineModuleFrontend::DisplayRole)
  133. {
  134. QWidget* widget = frontend->guiHandle()->findChild<QWidget*>("parameter:" + parameter);
  135. QVERIFY(widget != NULL);
  136. QString tag = this->ModuleRef.description().parameter(parameter).tag();
  137. if (tag == "integer")
  138. {
  139. QSpinBox* spinBox = qobject_cast<QSpinBox*>(widget);
  140. QVERIFY(spinBox);
  141. QCOMPARE(spinBox->value(), expectedValue.toInt());
  142. }
  143. else if (tag.endsWith("enumeration"))
  144. {
  145. QComboBox* comboBox = qobject_cast<QComboBox*>(widget);
  146. QVERIFY(comboBox);
  147. QCOMPARE(comboBox->currentText(), expectedValue.toString());
  148. }
  149. else
  150. {
  151. QFAIL("Missing widget sub-class test code.");
  152. }
  153. }
  154. }
  155. QCOMPARE(this->ChangedParameter, parameter);
  156. QCOMPARE(this->ChangedParameterValue, expectedValue);
  157. }
  158. }
  159. // ----------------------------------------------------------------------------
  160. void ctkCmdLineModuleFrontendQtGuiTester::testValueSetterAndGetter_data()
  161. {
  162. QTest::addColumn<QString>("parameter");
  163. QTest::addColumn<QVariant>("currentValue");
  164. QTest::addColumn<QVariant>("newValue");
  165. QTest::addColumn<QVariant>("expectedValue");
  166. QTest::addColumn<int>("role");
  167. QTest::newRow("intParamDefaultDefaultRole") << "intParam" << QVariant(1) << QVariant(2) << QVariant(2) << -1;
  168. QTest::newRow("intParamDefaultDisplayRole") << "intParam" << QVariant(1) << QVariant(2) << QVariant(2) << static_cast<int>(ctkCmdLineModuleFrontend::DisplayRole);
  169. QTest::newRow("intParmaDefaultLRRole") << "intParam" << QVariant(1) << QVariant(2) << QVariant(2) << static_cast<int>(ctkCmdLineModuleFrontend::LocalResourceRole);
  170. // newValue too low
  171. QTest::newRow("intParamTooLowDefaultRole") << "intParam" << QVariant(1) << QVariant(-6) << QVariant(-5) << -1;
  172. QTest::newRow("intParamTooLowDisplayRole") << "intParam" << QVariant(1) << QVariant(-6) << QVariant(-5) << static_cast<int>(ctkCmdLineModuleFrontend::DisplayRole);
  173. QTest::newRow("intParmaTooLowLRRole") << "intParam" << QVariant(1) << QVariant(-6) << QVariant(-5) << static_cast<int>(ctkCmdLineModuleFrontend::LocalResourceRole);
  174. // newValue too high
  175. QTest::newRow("intParamTooHighDefaultRole") << "intParam" << QVariant(1) << QVariant(200) << QVariant(60) << -1;
  176. QTest::newRow("intParamTooHighDisplayRole") << "intParam" << QVariant(1) << QVariant(200) << QVariant(60) << static_cast<int>(ctkCmdLineModuleFrontend::DisplayRole);
  177. QTest::newRow("intParmaTooHighLRRole") << "intParam" << QVariant(1) << QVariant(200) << QVariant(60) << static_cast<int>(ctkCmdLineModuleFrontend::LocalResourceRole);
  178. QTest::newRow("stringEnumDefaultRole") << "stringEnumParam" << QVariant("yes") << QVariant("no") << QVariant("no") << -1;
  179. QTest::newRow("stringEnumDisplayRole") << "stringEnumParam" << QVariant("yes") << QVariant("no") << QVariant("no") << static_cast<int>(ctkCmdLineModuleFrontend::DisplayRole);
  180. QTest::newRow("stringEnumLRRole") << "stringEnumParam" << QVariant("yes") << QVariant("no") << QVariant("no") << static_cast<int>(ctkCmdLineModuleFrontend::LocalResourceRole);
  181. QTest::newRow("intOutputParamDefaultRole") << "intOutputParam" << QVariant(0) << QVariant(3) << QVariant(3) << -1;
  182. QTest::newRow("intOutputParamDisplayRole") << "intOutputParam" << QVariant(0) << QVariant(3) << QVariant(3) << static_cast<int>(ctkCmdLineModuleFrontend::DisplayRole);
  183. QTest::newRow("intOutputParamLRRole") << "intOutputParam" << QVariant(0) << QVariant(3) << QVariant(3) << static_cast<int>(ctkCmdLineModuleFrontend::LocalResourceRole);
  184. }
  185. // ----------------------------------------------------------------------------
  186. CTK_TEST_MAIN(ctkCmdLineModuleFrontendQtGuiTest)
  187. #include "moc_ctkCmdLineModuleFrontendQtGuiTest.cpp"