ctkCmdLineModuleFrontendQtGuiTest.cpp 8.2 KB

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