ctkTreeComboBoxEventTranslatorPlayerTest1.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 <QAction>
  16. #include <QApplication>
  17. #include <QDebug>
  18. #include <QSignalSpy>
  19. #include <QStandardItemModel>
  20. #include <QTimer>
  21. #include <QTreeView>
  22. #include <QVBoxLayout>
  23. // CTK includes
  24. #include "ctkCallback.h"
  25. #include "ctkConfig.h"
  26. #include "ctkTreeComboBox.h"
  27. #include "ctkTreeComboBoxEventTranslator.h"
  28. #include "ctkTreeComboBoxEventPlayer.h"
  29. #include "ctkEventTranslatorPlayerWidget.h"
  30. // QtTesting includes
  31. #include "pqTestUtility.h"
  32. // STD includes
  33. #include <cstdlib>
  34. #include <iostream>
  35. namespace
  36. {
  37. QSignalSpy* Spy1;
  38. QSignalSpy* Spy2;
  39. //-----------------------------------------------------------------------------
  40. void checkFinalWidgetState(void* data)
  41. {
  42. ctkTreeComboBox* widget = reinterpret_cast<ctkTreeComboBox*>(data);
  43. CTKCOMPARE(Spy1->count(), 1);
  44. CTKCOMPARE(widget->currentText(), "Test1.2");
  45. }
  46. //-----------------------------------------------------------------------------
  47. void checkFinalWidgetState2(void* data)
  48. {
  49. QWidget* parentWidget = reinterpret_cast<QWidget*>(data);
  50. QList<ctkTreeComboBox*> widget = parentWidget->findChildren<ctkTreeComboBox*>();
  51. if(widget.count())
  52. {
  53. CTKCOMPARE(widget[0]->currentText(), "Test1.2");
  54. CTKCOMPARE(Spy2->count(), 3);
  55. }
  56. else
  57. {
  58. QApplication::exit(EXIT_FAILURE);
  59. }
  60. }
  61. }
  62. //-----------------------------------------------------------------------------
  63. int ctkTreeComboBoxEventTranslatorPlayerTest1(int argc, char * argv [] )
  64. {
  65. QApplication app(argc, argv);
  66. QString xmlDirectory = CTK_SOURCE_DIR "/Libs/Widgets/Testing/Cpp/";
  67. // ------------------------
  68. ctkEventTranslatorPlayerWidget etpWidget;
  69. pqTestUtility* testUtility = new pqTestUtility(&etpWidget);
  70. etpWidget.setTestUtility(testUtility);
  71. etpWidget.addWidgetEventTranslator(new ctkTreeComboBoxEventTranslator);
  72. etpWidget.addWidgetEventPlayer(new ctkTreeComboBoxEventPlayer);
  73. // Test case 1
  74. ctkTreeComboBox* widget = new ctkTreeComboBox();
  75. QStandardItemModel model;
  76. model.appendRow(new QStandardItem("Test1"));
  77. model.item(0)->appendRow(new QStandardItem("Test1.1"));
  78. model.item(0)->appendRow(new QStandardItem("Test1.2"));
  79. model.item(0)->appendRow(new QStandardItem("Test1.3"));
  80. model.appendRow(new QStandardItem("Test2"));
  81. model.appendRow(new QStandardItem("Test3"));
  82. widget->setModel(&model);
  83. QSignalSpy spy1(widget, SIGNAL(currentIndexChanged(int)));
  84. Spy1 = &spy1;
  85. etpWidget.addTestCase(widget,
  86. xmlDirectory + "ctkTreeComboBoxEventTranslatorPlayerTest1.xml",
  87. &checkFinalWidgetState);
  88. // Test case 2
  89. QWidget widget2(0);
  90. QVBoxLayout *layout= new QVBoxLayout;
  91. ctkTreeComboBox* combo = new ctkTreeComboBox();
  92. QStandardItemModel model2;
  93. model2.appendRow(new QStandardItem("Test1"));
  94. model2.item(0)->appendRow(new QStandardItem("Test1.1"));
  95. model2.item(0)->appendRow(new QStandardItem("Test1.2"));
  96. model2.item(0)->appendRow(new QStandardItem("Test1.3"));
  97. model2.appendRow(new QStandardItem("Test2"));
  98. model2.appendRow(new QStandardItem("Test3"));
  99. combo->setModel(&model2);
  100. layout->addWidget(combo);
  101. widget2.setLayout(layout);
  102. QSignalSpy spy2(combo, SIGNAL(currentIndexChanged(int)));
  103. Spy2 = &spy2;
  104. etpWidget.addTestCase(&widget2,
  105. xmlDirectory + "ctkTreeComboBoxEventTranslatorPlayerTest2.xml",
  106. &checkFinalWidgetState2);
  107. // ------------------------
  108. if (argc < 2 || QString(argv[1]) != "-I")
  109. {
  110. QTimer::singleShot(0, &etpWidget, SLOT(play()));
  111. }
  112. etpWidget.show();
  113. return app.exec();
  114. }