ctkWorkflowWidgetTest1.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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 <QApplication>
  16. #include <QDebug>
  17. #include <QIcon>
  18. #include <QLabel>
  19. #include <QLineEdit>
  20. #include <QList>
  21. #include <QPushButton>
  22. #include <QSignalSpy>
  23. #include <QStyle>
  24. #include <QTimer>
  25. // CTK includes
  26. #include "ctkWorkflow.h"
  27. #include "ctkWorkflowWidget.h"
  28. #include "ctkWorkflowStackedWidget.h"
  29. #include "ctkWorkflowTabWidget.h"
  30. #include "ctkWorkflowGroupBox.h"
  31. #include "ctkWorkflowButtonBoxWidget.h"
  32. #include "ctkExampleDerivedWorkflowWidgetStep.h"
  33. // STD includes
  34. #include <cstdlib>
  35. #include <iostream>
  36. //-----------------------------------------------------------------------------
  37. bool buttonClickTest(QApplication& app, int defaultTime, ctkWorkflowWidgetStep* currentStep, QWidget* shownStepArea, QLineEdit* shownLineEdit, QLabel* shownLabel, QWidget* hiddenStepArea, QLineEdit* hiddenLineEdit, QLabel* hiddenLabel, ctkWorkflow* workflow, ctkWorkflowWidget* workflowWidget, QPushButton* backButton, QPushButton* nextButton, QPushButton* finishButton1=0, QPushButton* finishButton2=0)
  38. {
  39. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  40. app.exec();
  41. // // ensure we are in the correct step
  42. if (workflow->currentStep() != currentStep)
  43. {
  44. std::cerr << "In incorrect step" << std::endl;
  45. return false;
  46. }
  47. // ensure that the widgets of the current step are visible
  48. if ((currentStep && (!shownStepArea || !shownStepArea->isVisible()))
  49. || (shownLabel && !shownLabel->isVisible())
  50. || (shownLineEdit && !shownLineEdit->isVisible())
  51. || !backButton->isVisible()
  52. || !nextButton->isVisible()
  53. || (finishButton1 && !finishButton1->isVisible())
  54. || (finishButton2 && !finishButton2->isVisible()))
  55. {
  56. std::cerr << "Incorrect widget visibility - the current step's widgets are invisible" << std::endl;
  57. return false;
  58. }
  59. // ensure that buttons are appropriately enabled
  60. // TODO finish buttons
  61. if ((workflow->canGoBackward() != backButton->isEnabled()) || (workflow->canGoForward() != nextButton->isEnabled()) || (shownLineEdit && !shownLineEdit->isEnabled()))
  62. {
  63. std::cerr << "Incorrect widget visibility - the buttons are incorrectly enabled" << std::endl;
  64. return false;
  65. }
  66. // ensure that the currentStep step's name and description are shown in
  67. // the widget
  68. ctkWorkflowGroupBox* groupBox;
  69. if (ctkWorkflowAbstractPagedWidget* pagedWidget = qobject_cast<ctkWorkflowAbstractPagedWidget*>(workflowWidget))
  70. {
  71. groupBox = pagedWidget->workflowGroupBox(currentStep);
  72. }
  73. else
  74. {
  75. groupBox = workflowWidget->workflowGroupBox();
  76. }
  77. Q_ASSERT(groupBox);
  78. if (currentStep->name() != groupBox->title() || currentStep->description() != groupBox->subTitle())
  79. {
  80. std::cerr << "Incorrect widget title/subtitle" << std::endl;
  81. return false;
  82. }
  83. // ensure that the finish button has an icon
  84. if ((finishButton1 && finishButton1->icon().isNull()) || (finishButton2 && finishButton2->icon().isNull()))
  85. {
  86. std::cerr << "Incorrect finish icon visibility" << std::endl;
  87. return false;
  88. }
  89. // ensure that widgets of the other step are either invisible, or
  90. // visible but disabled
  91. if (hiddenStepArea)
  92. {
  93. if (hiddenStepArea->isVisible() && groupBox->hideWidgetsOfNonCurrentSteps())
  94. {
  95. std::cerr << "Incorrect widget visibility - the other step's stepArea is showing" << std::endl;
  96. return false;
  97. }
  98. else if (hiddenStepArea->isVisible() && hiddenStepArea->isEnabled())
  99. {
  100. std::cerr << "Incorrect widget visibility - the other step's stepArea is enabled" << std::endl;
  101. return false;
  102. }
  103. }
  104. if (hiddenLabel)
  105. {
  106. if (hiddenLabel->isVisible() && groupBox->hideWidgetsOfNonCurrentSteps())
  107. {
  108. std::cerr << "Incorrect widget visibility - the other step's label is showing" << std::endl;
  109. return false;
  110. }
  111. }
  112. if (hiddenLineEdit)
  113. {
  114. if (hiddenLineEdit->isVisible() && groupBox->hideWidgetsOfNonCurrentSteps())
  115. {
  116. std::cerr << "Incorrect widget visibility - the other step's lineEdit is showing" << std::endl;
  117. return false;
  118. }
  119. else if (hiddenLineEdit->isVisible() && hiddenLineEdit->isEnabled())
  120. {
  121. std::cerr << "Incorrect widget visibility - the other step's lineEdit is enabled" << std::endl;
  122. return false;
  123. }
  124. }
  125. return true;
  126. }
  127. //-----------------------------------------------------------------------------
  128. struct derivedTestData
  129. {
  130. QPushButton* buttonToClick;
  131. ctkExampleDerivedWorkflowWidgetStep* currentStep;
  132. ctkExampleDerivedWorkflowWidgetStep* hiddenStep;
  133. ctkExampleDerivedWorkflowWidgetStep* stepToChangeLineEdit;
  134. QString lineEditText;
  135. derivedTestData(QPushButton* newButtonToClick, ctkExampleDerivedWorkflowWidgetStep* newCurrentStep, ctkExampleDerivedWorkflowWidgetStep* newHiddenStep, ctkExampleDerivedWorkflowWidgetStep* newStepToChangeLineEdit=0, QString newLineEditText="")
  136. {
  137. this->buttonToClick = newButtonToClick;
  138. this->currentStep = newCurrentStep;
  139. this->hiddenStep = newHiddenStep;
  140. this->stepToChangeLineEdit = newStepToChangeLineEdit;
  141. this->lineEditText = newLineEditText;
  142. }
  143. bool runTest(QApplication& app, int defaultTime, ctkWorkflow* workflow, ctkWorkflowWidget* workflowWidget, QPushButton* backButton, QPushButton* nextButton, QPushButton* finishButton1=0, QPushButton* finishButton2=0)
  144. {
  145. if (this->stepToChangeLineEdit)
  146. {
  147. this->stepToChangeLineEdit->lineEdit()->setText(lineEditText);
  148. }
  149. if (this->buttonToClick)
  150. {
  151. this->buttonToClick->click();
  152. }
  153. if (this->currentStep)
  154. {
  155. return buttonClickTest(app, defaultTime, this->currentStep, this->currentStep->stepArea(), this->currentStep->lineEdit(), this->currentStep->label(), this->hiddenStep->stepArea(), this->hiddenStep->lineEdit(), this->hiddenStep->label(), workflow, workflowWidget, backButton, nextButton, finishButton1, finishButton2);
  156. }
  157. else
  158. {
  159. return buttonClickTest(app, defaultTime, this->currentStep, 0, 0, 0, this->hiddenStep->stepArea(), this->hiddenStep->lineEdit(), this->hiddenStep->label(), workflow, workflowWidget, backButton, nextButton, finishButton1, finishButton2);
  160. }
  161. }
  162. };
  163. //-----------------------------------------------------------------------------
  164. // simulates valid and invalid user interaction for a workflow with
  165. // two steps
  166. int userInteractionSimulator1(QApplication& app, ctkExampleDerivedWorkflowWidgetStep* step1, ctkExampleDerivedWorkflowWidgetStep* step2, ctkWorkflow* workflow, ctkWorkflowWidget* workflowWidget, int defaultTime)
  167. {
  168. QPushButton* backButton = workflowWidget->buttonBoxWidget()->backButton();
  169. QPushButton* nextButton = workflowWidget->buttonBoxWidget()->nextButton();
  170. Q_ASSERT(backButton);
  171. Q_ASSERT(nextButton);
  172. #ifndef QT_NO_DEBUG
  173. QPushButton* finishButton = workflowWidget->buttonBoxWidget()->goToButtons().first();
  174. Q_ASSERT(finishButton);
  175. #endif
  176. QList<derivedTestData*> tests;
  177. // we should be in the first step at the start of the workflow
  178. tests << new derivedTestData(0, step1, step2);
  179. // tests with good input (lineEdit value = 100, which passes the
  180. // condition that it be >= 10)
  181. // CurrentStep ButtonPress ExpectedStep Shouldn'tDoAnything
  182. // step1 next step2
  183. // step2 back step1
  184. // step1 next step2
  185. // step2 back step1
  186. tests << new derivedTestData(nextButton, step2, step1)
  187. << new derivedTestData(backButton, step1, step2)
  188. << new derivedTestData(nextButton, step2, step1)
  189. << new derivedTestData(backButton, step1, step2);
  190. // tests with both good and bad input (lineEdit value may be
  191. // invalid)
  192. // CurrentStep ButtonPress Invalid input ExpectedStep
  193. // step1 next * step1
  194. // step1 next step2
  195. // step2 back * step1
  196. // step1 next * (empty) step1
  197. // step1 next step2
  198. tests << new derivedTestData(nextButton, step1, step2, step1, "1")
  199. << new derivedTestData(nextButton, step2, step1, step1, "100")
  200. << new derivedTestData(backButton, step1, step2)
  201. << new derivedTestData(nextButton, step1, step2, step1, "")
  202. << new derivedTestData(nextButton, step2, step1, step1, "100");
  203. foreach (derivedTestData* test, tests)
  204. {
  205. if (!test->runTest(app, defaultTime, workflow, workflowWidget, backButton, nextButton))
  206. {
  207. return EXIT_FAILURE;
  208. }
  209. }
  210. // TODO
  211. // // after adding the steps, then the widget's client area should have
  212. // // them as a child
  213. // if (!workflowWidget->clientArea()->isAncestorOf(testStep1->stepArea()))
  214. // {
  215. // std::cerr << "testStep1 was incorrectly added to the workflowWidget" << std::endl;
  216. // return EXIT_FAILURE;
  217. // }
  218. // if (!workflowWidget->clientArea()->isAncestorOf(testStep2->stepArea()))
  219. // {
  220. // std::cerr << "testStep2 was incorrectly added to the workflowWidget" << std::endl;
  221. // return EXIT_FAILURE;
  222. // }
  223. return EXIT_SUCCESS;
  224. }
  225. //-----------------------------------------------------------------------------
  226. // simulates valid and invalid user interaction for a workflow with
  227. // three steps
  228. int userInteractionSimulator2(QApplication& app, ctkExampleDerivedWorkflowWidgetStep* step1, ctkExampleDerivedWorkflowWidgetStep* step2, ctkExampleDerivedWorkflowWidgetStep* step3, ctkWorkflow* workflow, ctkWorkflowWidget* workflowWidget, int defaultTime)
  229. {
  230. // ensure we can get the pages/layouts we may want
  231. if (ctkWorkflowAbstractPagedWidget* pagedWidget = qobject_cast<ctkWorkflowAbstractPagedWidget*>(workflowWidget))
  232. {
  233. if (!pagedWidget->workflowGroupBox(step1))
  234. {
  235. std::cerr << "could not access widget for page 1" << std::endl;
  236. return EXIT_FAILURE;
  237. }
  238. if (!pagedWidget->workflowGroupBox(step1)->clientAreaLayout())
  239. {
  240. std::cerr << "could not access client area layout for page 1" << std::endl;
  241. return EXIT_FAILURE;
  242. }
  243. }
  244. else
  245. {
  246. if (!workflowWidget->workflowGroupBox()->clientAreaLayout())
  247. {
  248. std::cerr << "could not access client area layout" << std::endl;
  249. return EXIT_FAILURE;
  250. }
  251. }
  252. QPushButton* backButton = workflowWidget->buttonBoxWidget()->backButton();
  253. QPushButton* nextButton = workflowWidget->buttonBoxWidget()->nextButton();
  254. QPushButton* finishButton = workflowWidget->buttonBoxWidget()->goToButtons().first();
  255. Q_ASSERT(backButton);
  256. Q_ASSERT(nextButton);
  257. Q_ASSERT(finishButton);
  258. QList<derivedTestData*> tests;
  259. // we should be in the first step at the start of the workflow
  260. tests << new derivedTestData(0, step1, step2);
  261. // tests with good input (lineEdit value = 100, which passes the
  262. // condition that it be >= 10)
  263. // CurrentStep ButtonPress ExpectedStep Shouldn'tDoAnything
  264. // step1 next step2
  265. // step2 next step3
  266. // step3 back step2
  267. // step2 back step1
  268. // step1 next step2
  269. // step2 next step3
  270. // step3 back step2
  271. tests << new derivedTestData(nextButton, step2, step1)
  272. << new derivedTestData(nextButton, step3, step2, step2, "100")
  273. << new derivedTestData(backButton, step2, step3)
  274. << new derivedTestData(backButton, step1, step2)
  275. << new derivedTestData(nextButton, step2, step1)
  276. << new derivedTestData(nextButton, step3, step2)
  277. << new derivedTestData(backButton, step2, step3);
  278. // tests with both good and bad input (lineEdit value may be
  279. // invalid)
  280. // CurrentStep ButtonPress Invalid input ExpectedStep
  281. // step2 next * step2
  282. // step2 next step3
  283. // step3 back * step2
  284. // step2 next * (empty) step2
  285. // step2 next step3
  286. tests << new derivedTestData(nextButton, step2, step3, step2, "1")
  287. << new derivedTestData(nextButton, step3, step2, step2, "100")
  288. << new derivedTestData(backButton, step2, step3)
  289. << new derivedTestData(nextButton, step2, step3, step2, "")
  290. << new derivedTestData(nextButton, step3, step2, step2, "100");
  291. // first go back to the beginning
  292. tests << new derivedTestData(backButton, step2, step3, step2, "100")
  293. << new derivedTestData(backButton, step1, step2);
  294. // tests going to the finish step
  295. // CurrentStep ButtonPress Invalid input ExpectedStep
  296. // step1 finish step1
  297. // step1 finish step1
  298. // step1 next step2
  299. // step2 finish step2
  300. // step2 next step3
  301. // step3 finish step3
  302. // step3 back step2
  303. // step2 finish step2
  304. // step2 back step1
  305. // step1 finish step1
  306. tests << new derivedTestData(finishButton, step1, step3)
  307. << new derivedTestData(finishButton, step1, step3)
  308. << new derivedTestData(nextButton, step2, step1)
  309. << new derivedTestData(finishButton, step2, step3)
  310. << new derivedTestData(nextButton, step3, step2)
  311. << new derivedTestData(finishButton, step3, step2)
  312. << new derivedTestData(backButton, step2, step3)
  313. << new derivedTestData(finishButton, step2, step3)
  314. << new derivedTestData(backButton, step1, step2)
  315. << new derivedTestData(finishButton, step1, step3);
  316. // tests going to the finish step with invalid input
  317. // CurrentStep ButtonPress Invalid input ExpectedStep
  318. // step1 finish * (step2) step2
  319. // step2 next step3
  320. // step3 back step2
  321. // step2 finish * (step2) step2
  322. // step2 back step1
  323. // step1 finish * (step3) step1
  324. tests << new derivedTestData(finishButton, step2, step1, step2, "0")
  325. << new derivedTestData(nextButton, step3, step2, step2, "100")
  326. << new derivedTestData(backButton, step2, step3)
  327. << new derivedTestData(finishButton, step2, step3, step2, "0")
  328. << new derivedTestData(backButton, step1, step2, step2, "100") // reset text for step2
  329. << new derivedTestData(finishButton, step1, step3, step3, "0");
  330. foreach (derivedTestData* test, tests)
  331. {
  332. if (!test->runTest(app, defaultTime, workflow, workflowWidget, backButton, nextButton, finishButton))
  333. {
  334. return EXIT_FAILURE;
  335. }
  336. }
  337. // TODO
  338. // // after adding the steps, then the widget's client area should have
  339. // // them as a child
  340. // if (!workflowWidget->workflowGroupBox()->isAncestorOf(step3->stepArea()))
  341. // {
  342. // std::cerr << "step3 was incorrectly added to the workflowWidget" << std::endl;
  343. // return EXIT_FAILURE;
  344. // }
  345. return EXIT_SUCCESS;
  346. }
  347. // //-----------------------------------------------------------------------------
  348. // // simulates valid and invalid user interaction for a workflow with
  349. // // three steps and two finish steps
  350. // int userInteractionSimulator3(QApplication& app, ctkExampleDerivedWorkflowWidgetStep* step1, ctkExampleDerivedWorkflowWidgetStep* step2, ctkExampleDerivedWorkflowWidgetStep* step3, ctkWorkflow* workflow, ctkWorkflowWidget* workflowWidget, int defaultTime)
  351. // {
  352. // QPushButton* backButton = workflowWidget->buttonBoxWidget()->backButton();
  353. // QPushButton* nextButton = workflowWidget->buttonBoxWidget()->nextButton();
  354. // QPushButton* finishButton1 = workflowWidget->buttonBoxWidget()->goToButtons().first();
  355. // QPushButton* finishButton2 = workflowWidget->buttonBoxWidget()->goToButtons().at(1);
  356. // Q_ASSERT(backButton);
  357. // Q_ASSERT(nextButton);
  358. // Q_ASSERT(finishButton1);
  359. // Q_ASSERT(finishButton2);
  360. // // if (!nextButton || !finishButton || !finishButton || nextButton->text() != "Next" || finishButton->text() != "finish" || finishButton->text() != "finish")
  361. // // {
  362. // // std::cerr << "incorrect button assignment for step1" << std::endl;
  363. // // return EXIT_FAILURE;
  364. // // }
  365. // // we should be in the first step
  366. // if (!buttonClickTest(app, defaultTime, step1, step2, workflow, workflowWidget, backButton, nextButton, finishButton1, finishButton2)) {return EXIT_FAILURE;}
  367. // // tests with good input, so that we can get all of the buttons
  368. // // (lineEdit value = 100, which passes the condition that it be >= 10)
  369. // // CurrentStep ButtonPress ExpectedStep Shouldn'tDoAnything
  370. // // step1 next step2
  371. // // step2 next step3
  372. // // normal forwards/backwards transitions
  373. // nextButton->click();
  374. // QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  375. // app.exec();
  376. // // get the back/next buttons for the second step
  377. // // buttons = step2->stepArea()->findChildren<QPushButton*>();
  378. // // if (buttons.length() != 4)
  379. // // {
  380. // // std::cerr << "incorrect number of buttons for step2" << std::endl;
  381. // // std::cerr << "number of buttons = " << buttons.length() << ", expecting 4" << std::endl;
  382. // // return EXIT_FAILURE;
  383. // // }
  384. // // backButton = buttons.at(BACK_BUTTON_INDEX);
  385. // // nextButton = buttons.at(NEXT_BUTTON_INDEX);
  386. // // finishButton = buttons.at(FINISH_BUTTON_INDEX_1);
  387. // // finishButton = buttons.at(FINISH_BUTTON_INDEX_2);
  388. // // backButton = workflowWidget->buttonBoxWidget()->backButton();
  389. // // nextButton = workflowWidget->buttonBoxWidget()->nextButton();
  390. // // finishButton = workflowWidget->buttonBoxWidget()->goToButtons().first();
  391. // // finishButton = workflowWidget->buttonBoxWidget()->goToButtons().at(1);
  392. // // if (!backButton || !nextButton || !finishButton || !finishButton || backButton->text() != "Back" || nextButton->text() != "Next" || finishButton->text() != "finish" || finishButton->text() != "finish")
  393. // // {
  394. // // std::cerr << "incorrect button assignment for step2" << std::endl;
  395. // // return EXIT_FAILURE;
  396. // // }
  397. // if (!buttonClickTest(app, defaultTime, step2, step1, workflow, workflowWidget, backButton, nextButton, finishButton1, finishButton2)) {return EXIT_FAILURE;}
  398. // nextButton->click();
  399. // QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  400. // app.exec();
  401. // // get the back/next buttons for the third step
  402. // // buttons = step3->stepArea()->findChildren<QPushButton*>();
  403. // // if (buttons.length() != 3)
  404. // // {
  405. // // std::cerr << "incorrect number of buttons for step3" << std::endl;
  406. // // std::cerr << "number of buttons = " << buttons.length() << ", expecting 3" << std::endl;
  407. // // return EXIT_FAILURE;
  408. // // }
  409. // // backButton = buttons.at(BACK_BUTTON_INDEX);
  410. // // finishButton1 = buttons.at(FINISH_BUTTON_INDEX_1);
  411. // // finishButton2 = buttons.at(FINISH_BUTTON_INDEX_2);
  412. // // backButton = workflowWidget->buttonBoxWidget()->backButton();
  413. // // finishButton1 = workflowWidget->buttonBoxWidget()->goToButtons().first();
  414. // // finishButton2 = workflowWidget->buttonBoxWidget()->goToButtons().at(1);
  415. // // if (!backButton || !finishButton1 || !finishButton2 || backButton->text() != "Back" || finishButton1->text() != "finish" || finishButton2->text() != "finish")
  416. // // {
  417. // // std::cerr << "incorrect button assignment for step3" << std::endl;
  418. // // return EXIT_FAILURE;
  419. // // }
  420. // if (!buttonClickTest(app, defaultTime, step3, step2, workflow, workflowWidget, backButton, nextButton, finishButton1, finishButton2)) {return EXIT_FAILURE;}
  421. // // tests going to the finish step
  422. // // CurrentStep ButtonPress Invalid input ExpectedStep
  423. // // step3 back step2
  424. // // step2 back step1
  425. // // step1 finish1 step1
  426. // // step1 next step2
  427. // // step2 finish1 step2
  428. // // step2 finish2 step2
  429. // // step2 back step1
  430. // // step1 finish2 step1
  431. // // step1 next step2
  432. // backButton->click();
  433. // if (!buttonClickTest(app, defaultTime, step2, step3, workflow, workflowWidget, backButton, nextButton, finishButton1, finishButton2)) {return EXIT_FAILURE;}
  434. // backButton->click();
  435. // if (!buttonClickTest(app, defaultTime, step1, step2, workflow, workflowWidget, backButton, nextButton, finishButton1, finishButton2)) {return EXIT_FAILURE;}
  436. // finishButton1->click();
  437. // if (!buttonClickTest(app, defaultTime, step1, step3, workflow, workflowWidget, backButton, nextButton, finishButton1, finishButton2)) {return EXIT_FAILURE;}
  438. // nextButton->click();
  439. // if (!buttonClickTest(app, defaultTime, step2, step1, workflow, workflowWidget, backButton, nextButton, finishButton1, finishButton2)) {return EXIT_FAILURE;}
  440. // finishButton1->click();
  441. // if (!buttonClickTest(app, defaultTime, step2, step3, workflow, workflowWidget, backButton, nextButton, finishButton1, finishButton2)) {return EXIT_FAILURE;}
  442. // finishButton2->click();
  443. // if (!buttonClickTest(app, defaultTime, step2, step3, workflow, workflowWidget, backButton, nextButton, finishButton1, finishButton2)) {return EXIT_FAILURE;}
  444. // backButton->click();
  445. // if (!buttonClickTest(app, defaultTime, step1, step2, workflow, workflowWidget, backButton, nextButton, finishButton1, finishButton2)) {return EXIT_FAILURE;}
  446. // finishButton2->click();
  447. // if (!buttonClickTest(app, defaultTime, step1, step2, workflow, workflowWidget, backButton, nextButton, finishButton1, finishButton2)) {return EXIT_FAILURE;}
  448. // nextButton->click();
  449. // if (!buttonClickTest(app, defaultTime, step2, step1, workflow, workflowWidget, backButton, nextButton, finishButton1, finishButton2)) {return EXIT_FAILURE;}
  450. // return EXIT_SUCCESS;
  451. // }
  452. //-----------------------------------------------------------------------------
  453. int runWorkflowWidgetTest(ctkWorkflowWidget* workflowWidget, QApplication& app, bool hideWidgets, int defaultTime)
  454. {
  455. ctkWorkflow* workflow = new ctkWorkflow;
  456. workflowWidget->setWorkflow(workflow);
  457. ctkWorkflowGroupBox* groupBox = workflowWidget->workflowGroupBox();
  458. groupBox->setPreText("I am some pre-text");
  459. groupBox->setPostText("I am some post-text");
  460. groupBox->setHideWidgetsOfNonCurrentSteps(hideWidgets);
  461. // create and add the first workflow step (depends on workflowWidget
  462. // type)
  463. ctkExampleDerivedWorkflowWidgetStep* step1 = new ctkExampleDerivedWorkflowWidgetStep("Step 1");
  464. step1->setName("Step 1");
  465. step1->setDescription("I am in step 1");
  466. if (ctkWorkflowTabWidget* tabWidget = qobject_cast<ctkWorkflowTabWidget*>(workflowWidget))
  467. {
  468. tabWidget->associateStepWithLabel(step1, "tab1");
  469. }
  470. // step1 is the initial step
  471. workflow->setInitialStep(step1);
  472. // create and add the second workflow step (depends on
  473. // workflowWidget type)
  474. ctkExampleDerivedWorkflowWidgetStep* step2 = new ctkExampleDerivedWorkflowWidgetStep("Step 2");
  475. step2->setName("Step 2");
  476. step2->setDescription("I am in step 2");
  477. if (ctkWorkflowTabWidget* tabWidget = qobject_cast<ctkWorkflowTabWidget*>(workflowWidget))
  478. {
  479. tabWidget->associateStepWithLabel(step2, "tab2");
  480. }
  481. int expectedStepCount = 0;
  482. int currentStepCount = workflow->steps().count();
  483. if (currentStepCount != expectedStepCount)
  484. {
  485. std::cerr << "Line " << __LINE__ << " - Problem with steps()\n"
  486. << "\tcurrentStepCount: " << currentStepCount << "\n"
  487. << "\texpectedStepCount:" << expectedStepCount << std::endl;
  488. return EXIT_FAILURE;
  489. }
  490. QSignalSpy signalSpyStepRegistered(workflow, SIGNAL(stepRegistered(ctkWorkflowStep*)));
  491. // add the steps to the workflow
  492. workflow->addTransition(step1, step2);
  493. expectedStepCount = 2;
  494. currentStepCount = workflow->steps().count();
  495. if (currentStepCount != expectedStepCount)
  496. {
  497. std::cerr << "Line " << __LINE__ << " - Problem with steps()\n"
  498. << "\tcurrentStepCount: " << currentStepCount << "\n"
  499. << "\texpectedStepCount:" << expectedStepCount << std::endl;
  500. return EXIT_FAILURE;
  501. }
  502. int expectedSignalStepRegisteredCount = 2;
  503. int currentSignalStepRegisteredCount = signalSpyStepRegistered.count();
  504. if (currentSignalStepRegisteredCount != expectedSignalStepRegisteredCount)
  505. {
  506. std::cerr << "Line " << __LINE__ << " - Problem with 'stepRegistered' signal\n"
  507. << "\tcurrentSignalStepRegisteredCount: " << currentSignalStepRegisteredCount << "\n"
  508. << "\texpectedSignalStepRegisteredCount:" << expectedSignalStepRegisteredCount << std::endl;
  509. return EXIT_FAILURE;
  510. }
  511. // start the workflow
  512. workflow->start();
  513. workflowWidget->show();
  514. // Attempt to add step to a different workflow
  515. ctkWorkflow* workflow2 = new ctkWorkflow;
  516. workflow2->addTransition(step1, step2);
  517. expectedStepCount = 0;
  518. currentStepCount = workflow2->steps().count();
  519. if (currentStepCount != expectedStepCount)
  520. {
  521. std::cerr << "Line " << __LINE__ << " - Problem with steps()\n"
  522. << "\tcurrentStepCount: " << currentStepCount << "\n"
  523. << "\texpectedStepCount:" << expectedStepCount << std::endl;
  524. return EXIT_FAILURE;
  525. }
  526. // first user interaction test
  527. if (userInteractionSimulator1(app, step1, step2, workflow, workflowWidget, defaultTime) == EXIT_FAILURE)
  528. {
  529. return EXIT_FAILURE;
  530. }
  531. // stop the workflow
  532. workflow->stop();
  533. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  534. app.exec();
  535. // create and add a third workflow step (depends on workflowWidget
  536. // type)
  537. ctkExampleDerivedWorkflowWidgetStep* step3 = new ctkExampleDerivedWorkflowWidgetStep("Step 3");
  538. step3->setName("Step 3");
  539. step3->setDescription("I am in step 3");
  540. if (ctkWorkflowStackedWidget* stackedWidget = qobject_cast<ctkWorkflowStackedWidget*>(workflowWidget))
  541. {
  542. stackedWidget->associateStepWithPage(step3, 1);
  543. }
  544. else if (ctkWorkflowTabWidget* tabWidget = qobject_cast<ctkWorkflowTabWidget*>(workflowWidget))
  545. {
  546. tabWidget->associateStepWithPage(step3, 1, "tab2");
  547. }
  548. // icon test - add an icon to step3, should show up as a icon on the finish button
  549. step3->setIcon(step3->stepArea()->style()->standardIcon(QStyle::SP_ArrowUp));
  550. workflow->addTransition(step2, step3);
  551. // restart the workflow
  552. workflow->start();
  553. // second user interaction test
  554. if (userInteractionSimulator2(app, step1, step2, step3, workflow, workflowWidget, defaultTime) == EXIT_FAILURE)
  555. {
  556. return EXIT_FAILURE;
  557. }
  558. // stop the workflow
  559. workflow->stop();
  560. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  561. app.exec();
  562. // TODO put back once we can have more than one finish step
  563. // make the second workflow step a finish step as well
  564. // finishSteps.push_back(step2);
  565. // step1->setFinishStepsToHaveButtonsInStepArea(finishSteps);
  566. // step2->setFinishStepsToHaveButtonsInStepArea(finishSteps);
  567. // step3->setFinishStepsToHaveButtonsInStepArea(finishSteps);
  568. // // workflow->addFinishStep(step2);
  569. // // restart the workflow
  570. // workflow->start();
  571. // QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  572. // app.exec();
  573. // // third user interfaction test
  574. // if (userInteractionSimulator3(app, step1, step2, step3, workflow, workflowWidget, defaultTime) == EXIT_FAILURE)
  575. // {
  576. // return EXIT_FAILURE;
  577. // }
  578. // // stop the workflow
  579. // workflow->stop();
  580. // QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  581. // app.exec();
  582. // handles deletion of the workflowWidget, workflow, steps, states
  583. // and transitions
  584. delete workflowWidget;
  585. return EXIT_SUCCESS;
  586. }
  587. //-----------------------------------------------------------------------------
  588. int ctkWorkflowWidgetTest1(int argc, char * argv [] )
  589. {
  590. QApplication app(argc, argv);
  591. int defaultTime = 100;
  592. bool hideWidgets = false;
  593. if (runWorkflowWidgetTest(new ctkWorkflowWidget, app, hideWidgets, defaultTime) == EXIT_FAILURE)
  594. {
  595. return EXIT_FAILURE;
  596. }
  597. if (runWorkflowWidgetTest(new ctkWorkflowStackedWidget, app, hideWidgets, defaultTime) == EXIT_FAILURE)
  598. {
  599. return EXIT_FAILURE;
  600. }
  601. if (runWorkflowWidgetTest(new ctkWorkflowTabWidget, app, hideWidgets, defaultTime) == EXIT_FAILURE)
  602. {
  603. return EXIT_FAILURE;
  604. }
  605. hideWidgets = true;
  606. if (runWorkflowWidgetTest(new ctkWorkflowWidget, app, hideWidgets, defaultTime) == EXIT_FAILURE)
  607. {
  608. return EXIT_FAILURE;
  609. }
  610. if (runWorkflowWidgetTest(new ctkWorkflowStackedWidget, app, hideWidgets, defaultTime) == EXIT_FAILURE)
  611. {
  612. return EXIT_FAILURE;
  613. }
  614. if (runWorkflowWidgetTest(new ctkWorkflowTabWidget, app, hideWidgets, defaultTime) == EXIT_FAILURE)
  615. {
  616. return EXIT_FAILURE;
  617. }
  618. return EXIT_SUCCESS;
  619. }